14

The following code is throwing an error message and I can't figure out what the issue is - is it the word static, or const? What am I doing wrong?

#include <iostream>
using namespace std;

class SampleClass
{
private:

    int value;
    static int counter;

public:

    SampleClass(int i)
    {
        value = i; 
        counter++;
    }

    static int countSomeClass() const
    {
        return counter;
    }

    void showValue()
    {
        cout << value << endl;
    }

};

int main()
{
    SampleClass test(50);
    test.showValue();
    test.countSomeClass();
    return 0;
}

Error message:

main.cpp:16:35: error: static member function static int SampleClass::countSomeClass() cannot have cv-qualifier
static int countSomeClass() const

3
  • 20
    A static function cannot be const. It is the combination that is the problem.
    – Niall
    Commented Oct 23, 2014 at 14:46
  • access specifier - read about those
    – ha9u63a7
    Commented Oct 23, 2014 at 14:50
  • 1
    @hagubear: How is that relevant? The function is public, so access isn't the problem. Commented Oct 23, 2014 at 14:51

4 Answers 4

23

A static method cannot be marked as const: since it doesn't work on an instance, it makes no sense to specify that it cannot modify it.

(you could argue that for static methods it could have referred to static methods that cannot modify the static data associated with the class; however, this would have no use anyway, since you cannot have a const class or form a const pointer or reference to a class, as in C++ classes aren't objects)

2
  • after removing const the following message appears /tmp/ccYI99mO.o: In function SampleClass::SampleClass(int)': main.cpp:(.text._ZN11SampleClassC2Ei[_ZN11SampleClassC5Ei]+0x16): undefined reference to SampleClass::counter' Commented Oct 23, 2014 at 14:59
  • 2
    @YelizavetaYR; your counter is declared but not defined (outside the class). It requires something like int SampleClass::counter = 0; in one of the translation units (cpp file).
    – Niall
    Commented Oct 23, 2014 at 15:05
13

As the error message says, a static member function can't have a cv-qualifier - that is, a const or volatile qualifier. Such qualifiers are applied to this within a non-static member; they make no sense on a static member which has no this.

You want the function to be static, since it doesn't depend on any instance; so remove the const.

12

A static function cannot be const (cv-qualified) and vice-versa. It is the combination of the two that is the problem.

From the C++ specification § 9.4.1/2 (emphasis mine)

[ Note: A static member function does not have a this pointer (9.3.2). —end note ] A static member function shall not be virtual. There shall not be a static and a non-static member function with the same name and the same parameter types (13.1). A static member function shall not be declared const, volatile, or const volatile.

A static function has no this access since it is not associated with a specific instance of an object. There is no need for a const to be applied, it wouldn't make sense - there is no instance of an object to apply the const to. In a similar way, a free function cannot be const (or cv-qualified).

If you wish the function to be associated with an object, remove the static, if you want it to be static remove the const - from your sample, remove the const.

One more note; your counter is declared but not defined (outside the class). It requires something like int SampleClass::counter = 0; in one of the translation units (cpp file).

5

It's related to both.

  • A static member function does not work on an instance of the class it's a member of.
  • The const keyword at the end of a member function signature indicates that this member function does not modify the instance it's called on.

So it makes no sense (and is thus invalid) to have both in the same member function declaration.

Not the answer you're looking for? Browse other questions tagged or ask your own question.