Introduction
When you try to use a member function on an object, the compiler checks to see if the object has type qualifiers that are compatible with the member function. For example, if you have a const member function, the compiler will only allow you to use that function on a const object.
What are type qualifiers?
Type qualifiers are special keywords that you can use to specify how a data type can be used. For example, the const keyword is a type qualifier that specifies that a data type is constant. That is, once you have initialized a const data type, you cannot change its value.
There are three type of type qualifiers:
1) const – to make an object constant
2) volatile – to specify that an object may change in an unknown way
3) restrict – to specify that an object should not overlap with another object
In order for a member function to access a const object, the member function must also be declared as const. This is because the member function is not allowed to change the value of the const object.
Type qualifiers and member functions
Type qualifiers and member functions can be a source of confusion and errors when programming in C++. In particular, the type qualifiers const and volatile have different meanings when applied to member functions.
The const type qualifier on a member function indicates that the function will not modify the object for which it is called. This is different from a non-const member function, which can modify the object. For example, consider the following class:
class MyClass
{
public:
void func1() const; // will not modify MyClass object
void func2(); // can modify MyClass object
};
attempt to call func1() on a const MyClass object, the compiler will generate an error. This is because the const type qualifier on func1() prevents it from modifying the const MyClass object.
Conversely, if you attempt to call func2() on a const MyClass object, the compiler will not generate an error. This is because the const type qualifier on func2() does not prevent it from modifying the const MyClass object.
Incompatible type qualifiers
When you use a member function of a class, the compiler verifies that the type of the object on which you are invoking the function is compatible with the type qualifiers of the function. For example, consider the following declarations:
class A {
public:
void f();
};
class B {
public:
void f() const;
};
int main() {
A a;
const B b;
a.f(); // OK: non-const member function invoked on non-const object
b.f(); // error: const member function invoked on const object
return 0; }
In the example above, a.f() is legal because A::f() does not have any type qualifiers and can be invoked on any object of class A, whether it is const or not. However, invoking B::f() on b results in an error because B::f() has the const qualifier and can therefore only be invoked on objects of class B that are declared as const.
Conclusion
This error means that you are trying to use a member function of an object, but the object has type qualifiers (such as const or volatile) that are not compatible with the member function. For example, if you have a const object, you can only call const member functions on that object.