Not an lvalue error

Hi,

I am a beginner in c++ and trying to learn the language through practice. I am getting this error "Expression must be a modifiable lvalue" for one of my programs.

bool Maze::set(int i, int j) {
if(grid.getAt(i,j).included == 0)
grid.getAt(i,j).included = 1; // get error at this point.
}

Grid is a custom template class that maintains a two dimensional array.
Maze class contains a grid member of type cell. cell is a struct defined as

struct cell {
int walls[4];
bool included;
};
class Maze {
public:
bool set(int i, int j);
private:
Grid<cell> grid;
};

I want to change the 'included' variable but I am getting this error. If I try to change the walls value grid.getAt(i,j).walls[1] = 1 then the compiler allows me to do so.

Please explain why the compiler is complaining for changing included.
Return the cell by reference.
Thanks a lot Peter!

It did solve the problem.

Can you tell me why could not I set the variable on the copy? Is it that I was referencing to a copy directly without storing it and that's why it was kind of a constant which didn't allow any changes?
It would be useless if you could change the temporary object directly like that. In the next statement the temporary object would be gone anyway. It is much better that it gives you an error so that you see that something is wrong right away, otherwise you would notice that something is wrong later on and have to do much more debugging to find out where the problem is.
Last edited on
Thanks a lot Peter! Much appreciated.
Topic archived. No new replies allowed.