ERROR: Expression Must Be a Modifiable lvalue

I've written a linked list and once I got to the end, it states that my list variable has the above error (within title). Not sure on what is wrong.

HEADER FILE:
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
#ifndef DoublyLinkedList_h
#define DoublyLinkedList_h

//typedef struct Node;

struct Node
{
	int data;
	Node *next,*previous;
	Node (int a, Node *pre, Node *next) : data(a), previous(pre), next(next)
	{
		if(pre) previous -> next = this;
		if (next) next -> previous = this;
	}
};

class DoublyLinkedList
{
public: 
	DoublyLinkedList(int);
	DoublyLinkedList();
	~DoublyLinkedList();
	Node *head;
	int size;
	void addNodeBefore (int);
	void addNodeAfter (int);
	void addNodeBeforeData (int, Node*);
	void addNodeAfterData (int, Node*);
	//void out(bool);
	void setData (int);
	void setNext (Node*);
	void setPrevious (Node*);
	void deleteData (int, bool);
	bool empty();
	bool findData(int);
};
#endif 


.CPP FILE:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <string>
#include <iomanip> 
#include "DoublyLinkedList.h"
using namespace std;

DoublyLinkedList::DoublyLinkedList (int data)
{
	head = new Node (data, NULL, NULL);
	head ->next = head ->previous = head;
	size = 3;
}

DoublyLinkedList::DoublyLinkedList()
{
	Node *temp = this->head;
	Node *temp2;
	while (temp->previous)
		temp = temp ->previous;
	while(temp)
	{
		temp2 = temp ->next;
		delete temp;
		temp = temp2;
	}
	temp = temp2 = NULL;
}

bool DoublyLinkedList::empty()
{
	return (this ->size == 0);
}

void DoublyLinkedList ::addNodeBefore (int data)
{
	Node *next = head;
	Node *previous = head ->previous;

	Node *temp2 = new Node (data, previous, next);
	size++
}

void DoublyLinkedList::addNodeAfter(int data)
{
	Node *next = head ->next;
	Node *previous = head;

	Node *temp2 = new Node (data, previous, next);
	size++;
}

/*void DoublyLinkedList::out(bool directly)
{
	if (directly)
	{
		Node *temp = head;
		do 
		{
			cout<<temp ->data;
			temp = temp->next;
		}
		while (temp != head);
	}
	else
	{
		Node *temp = head;
		do
		{
			cout<<temp->data;
			temp = temp ->previous;
		}
		while (temp != head);
	}
	cout<<endl;
}*/

void DoublyLinkedList::setData( int Data)
{
	this ->head->data = Data;
}
void DoublyLinkedList::setPrevious(Node* Previous)
{
	this ->head->previous = Previous;
}
void DoublyLinkedList::setNext( Node* Next)
{
	this ->head->next = Next;
}

bool DoublyLinkedList::findData (int find)
{
	int counterStart = 0;
	Node *temp = head;
	while (temp ->next != head)
	{
		if (temp ->data == find)
			counterStart++;
		temp = temp->next;
	}

	if (counterStart > 0)
	{
		cout<<" ' "<<find<<" ' was found"<<counterStart<<" time(s)"<<endl;
		return true;
	}
	else
	{
		cout<<" ' "<<find<<" ' was not found"<<endl;
		return false;
	}
}

void DoublyLinkedList::deleteData(int find, bool everything)
{
	Node *temp = head;
	while (temp)
	{
		if (temp ->data == find)
		{
			cout<<"Removing "<<find<<endl;
			temp ->previous->next = temp ->next;
			temp ->next->previous = temp ->previous;
			if (false)
				return;
		}
		temp = temp ->next;
	}
}


TESTER FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <iomanip> 
#include "DoublyLinkedList.h"

using namespace std;

int main ()
{
	int list [] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31};

	DoublyLinkedList *list;
	Node *current;

	list = new DoublyLinkedList (list[0]);

	for (size_t i = 1; i < strlen(list); i++)
		list -> addNodeAfter (list[i]);


system ("pause");
	return 0;
}
I didn't go through the entire code, but see the line no 10 and 12 in tester file. You have used the same name.
Last edited on
haven't gone through your code in detail but one error that stands out is
1
2
3
int list [] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31};

DoublyLinkedList *list; // double declaration of same identifier?? 

I've written a linked list and once I got to the end, it states that my list variable has the above error (within title).


Errors generally come with a line number indicating which line the error occurred on. It would be helpful if you supplied the full text of the error message including the line number.
Thank you @kulkarnisr and @abhishekm71 ! I've fixed line 12, which fixed lines 15 and half of 18 .

@Cire: It was an error mentioned when I hovered over the variable when it came to line 12. Sorry for not mentioning that

Once I fixed the 3 lines, line 17 and the latter part of 18 are still not happy. Line 17 error states " 'strlen' : cannot convert parameter 1 from 'int [16]' to 'const char ' "
And line 18 error reads as " IntelliSense: no suitable conversion function from "DoublyLinkedList" to "int" exist "

CORRECTED THE CODE AS SO:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main ()
{
	int providedNumbers [] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31};

	DoublyLinkedList *list;
	Node *current;

	list = new DoublyLinkedList (list[0]);

	for (size_t i = 1; i < strlen(list); i++)
		list -> addNodeAfter (list[i]);


system ("pause");
	return 0;
}

you need to change the use of the variable as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main ()
{
	int providedNumbers [] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31};

	DoublyLinkedList *list;
	Node *current;

	list = new DoublyLinkedList (providedNumbers[0]);

	for (size_t i = 1; i < sizeof(providedNumbers) / sizeof(*providedNumbers); i++) // NOTE: strlen doesn't make sense here!
		list -> addNodeAfter (providedNumbers[i]);


system ("pause");
	return 0;
}
1
2
3
4
5
6
7
8
list = new DoublyLinkedList (list[0]); // what is list[0]? not defined.

for (size_t i = 1; i < strlen(list); i++)
  list -> addNodeAfter (list[i]);
// list is a linked list. strlen(list) is incorrect. You will have to keep a count of
// the number of elements in the list. Also, you cannot directly access a node in the
// list by list[i]. You will have to iterate through all the nodes.
Last edited on
Topic archived. No new replies allowed.