I’m getting this error message when try to compile the following
void MyClass::MyFunc() const
{
int i= 0;
myList.push_back(i);
}
I’ve already had to add the const to the function, despite it returning void, to fix an identical problem with the invocation of the function itself, but now that it’s occurring with a member of the STL, I’m thinking something is very wrong with my code. For reference, the error was
cannot convert 'this' pointer from 'const MyClass' to 'MyClass &'
I actually have very little idea of what’s going on since I inherited this from someone with a very abusive relationship with coding standards and best practices. If someone could tell me what problem this is a symptom of and what I should be looking for in the code, it would help me a lot.
conston a method means that it can’t modify the instance it is called on, including any instance members.You need to take a look at the code base and distinguish between methods that can modify the class instance (which should not be
const) and those that just provide information about the class instance (which should beconst).