Expression must be a modifiable lvalue

I am getting a very strange error when I am simply trying to modify an array within a class. In my .h file, I have initialized an array, and in my .cpp file, I have a function that modifies a single element in that array. However, I get this error when I try to do that. Here is the code:

.h File
1
2
3
4
5
6
7
8
9
10
11
class TheList
{
public:
	TheList();
	bool Insert(const int &index, const int &value) const;  

	~TheList();

private:
	int list[10];
};


.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
TheList::TheList()
{
}

bool TheList::Insert(const int &index, const int &value) const
{
	list[index] += value; // <---- error is here, Expression must be a modifiable lvalue
	return true;
}

TheList::~TheList()
{
}


Last edited on
Are you sending a value to index greater than 9 (since index of array would go from 0 - 9 )??

and do you need to send both the parameters by reference since neither index or value are being changed. Not sure if that is your error, or if the const keyword is causing it...but something to try.

I actually haven't sent any values yet. As I was coding, the red squiggly lines and the error showed up under that line.

I'm setting both to a constant reference so they can't be changed. I tried just putting "int index, int value", but nothing changed
Last edited on
you aren't initializing your array...

try adding this to your constructor

1
2
3
4
5
TheList::TheList()
{
  for(int i=0; i<10; i++)
    list[i] = 0;
}
I added that for loop to my constructor, and the error persists.
sorry, should have seen it before...I was on the right track =P

need to change

bool Insert(const int &index, const int &value) const;
to ...

bool Insert(const int &index, const int &value);
and also in the .cpp file at the definition. constant functions aren't allowed to change member information.

Do keep the constructor code to initialize the array before trying to append any new numbers to it.
Last edited on
Topic archived. No new replies allowed.