Here is the problem. I wrote this function to return a reference to the i element of a member vector, so this element could be edited. Here is the code:
Letter& Literal::get (int i) const {
return lit_m.at (i); //Vector of Letter objects
}
But g++ won't let me assign that element to a non-const reference:
g++ -o literal.o -c literal.cpp
literal.cpp: In member function ‘Letter& Literal::get(int) const’:
literal.cpp:34: error: invalid initialization of reference of type ‘Letter&’ from expression of type ‘const Letter’
How could it be solved? My idea is to build up a function like the at() function of vectors, so it would be const as it doesn't edit the object itself, but it should let me edit the returned object...Is it possible?
SOLVED: I just had to overload the function :), so declare a const and a non-const version. I was afraid that const and non-const overloading wasn't permitted, but I saw that that const changes the argument list, making it possible.