Making a function const does not mean that its return is const, it means that you actually have to mark the function as being a constant function.
In other words, doing this is not enough, since the compiler has no idea if the function modifies the "this" pointer.
class Foo{public: void doSomething(); //Does this modify data? We have no idea.private: int data;};
But if you mark it as const, then the compiler knows that nothing* in the class can be changed when the function is executing.
class Foo{public: void doSomethingConst() const; //We don't modify data in this function!private: int data;}
The practical upshot of all this is that you can only call const functions from a const instance.
Foo nonConst;const Foo constFoo;nonConst.doSomething(); //FinenonConst.doSomethingConst(); //FineconstFoo.doSomethingConst(); //FineconstFoo.doSomething(); //FAIL! You cannot call a non-const function from a const instance.
So, in your code, you have a
const Var& val;//And you attempt to call Var::toString(); which is not const. You have to define it as a const function, by appending a const to the end of the function declaration.
* except for mutable members, but thats kinda a non-issue here.
EDIT:
Well, if thats not the problem, you might have to paste more code so we can see whats actually going wrong.