problem with lvalue

I get two errors:
error C2106: '=' : left operand must be l-value
IntelliSense: expression must be a modifiable lvalue

Here is my code Head.cpp

1
2
3
4
 void List::vklad(int ciastka, List& L){
	L.Current->getStav() = L.Current->getStav() + ciastka;   //there are the errors on L
	cout << "Vklad na ucet cislo:" << L.Current->getId() << endl;
}


and Head.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class ListItem
{
private:
	int id;
	int stav;
	int urok;
	int Q;
	ListItem* Prev;
	ListItem* Next;
public:
	ListItem(int ide, ListItem* Head);
	ListItem* getNext();
	ListItem* getPrev();
	int getStav();
	int getId();

};


class List
{
private:
	ListItem* Head;
	
	ListItem* Current;

public:
	List();
	void Insert(List& L,int ide);
	void MoveNext(List& L);
	ListItem* getHead();
	void Clear(List& L);
	ListItem* getCurrent();
	void vyber(int ciastka, List& L);
	void vklad(int ciastka, List& L);
};


Thanks for help
One error; two lines explaining it.

You ListItem is inmutable; its interface does not let changing the item. (It has only "getters" and no "setters".)


The ListItem::getStav() returns an int. A copy. An unnamed temporary variable. An r-value.

On the left side of operator= one has to have something that one can write to. Store to. Something that can then be used. An l-value. You cannot use a temporary unnamed int in following statements.


PS. Why is the vklad() a member of the List? It operates only on the function arguments. It could be a standalone function. (A standalone function can be part of the logical interface of a class.)
I repair the "setters" many errors are gone but i got some other problem


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Head.h

class ListItem
{
private:
	int id;
	int stav;
	int urok;
	int Q;
	ListItem* Prev;
	ListItem* Next;
public:
	ListItem(int ide,List& L); //here
	ListItem* getNext();
	ListItem* getPrev();
	int getStav();
	int getId();
	int getQ();
	int getUrok();

	void setUrok();
	void setStav();

	void vyber(int ciastka);
	void vloz(int ciastka);

};

class List
{
private:
	ListItem* Head;

	ListItem* Current;

public:
	List();

	void Insert(List& L, int ide);
	void MoveNext(List& L);
	ListItem* getHead();
	void Clear(List& L);
	ListItem* getCurrent();

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Head.cpp

ListItem::ListItem(int ide,List& L){
	
	L.getCurrent()->id = ide;
	
	L.getCurrent()->stav = rand() % M;
	
	L.getCurrent()->urok = rand() % P + 1;
	
	if ((L.getCurrent()->stav*(L.getCurrent()->urok / 100))*0.2 < 500){ L.getCurrent()->Q = 500; }
	else{ L.getCurrent()->Q = (L.getCurrent()->stav*(L.getCurrent()->urok / 100))*0.2; }

	L.getCurrent()->Prev = NULL;
	

}


error C2061: syntax error : identifier 'List'


Thanks for help.
Last edited on
The compiler has no idea what the "List" is on line 13. By the time it does reach line 30, it does, but that is too late.

One has to forward declare:
1
2
3
4
5
6
7
8
9
class List; // identifier "List" means a class type

class ListItem {
  // it is now ok to use identifier "List", unless its size is needed.
  // Pointers and references don't need size of type.
};

class List {
};
Topic archived. No new replies allowed.