Linked List error

Hi all,

I coded a single Linked list with delete function. I am using class not struct to define the node. I am getting weird error called:

1
2
error: expression is not assignable
        pre->Next() = cur->Next();


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
void LinkedList::Delete(int number){
	
	Node* cur = head;
	Node* pre = NULL;
	int count = 1;

	if(head == NULL)
		return;
	
	while(cur->Next() != NULL && number!=count)
	{
		pre = cur;
		cur = cur -> Next();
		count++;
	}

	if(cur->Next() == NULL)
	{
		cout << "Linked list is Empty" << endl;
		return;
	}

    if(cur == head) {
        // Change head to point to next node
        head = head->Next();
    }
    else {
        // Skip the current node
        pre->Next() = cur->Next();
    }

    // Free space used by deleted node
    delete cur;
    std::cout << "Deleted succesfully" << std::endl;
}


thanks in advance
Last edited on
pre->Next() is a function call. The simplistic answer is that it makes no sense to try and set a function call to another value.

For a slightly more complex answer:

You don't show us the definition of Node::Next(), but presumably it's returning the value of the pointer to the next node. That return value is an unnamed, temporary variable. It exists only in that moment of being returned from the function. You can use the value - e.g. to assign it to another variable - but it makes no sense to try and change the value, because it's only temporary.

We call this an rvalue, because it can only appear on the right-hand side of an assignment; you can read its value, but you can't change it.

A named variable is an lvalue, because it can appear on the left-hand side of an assignment; you can change its value.

If this doesn't make sense, I suggest you go to your textbook, or go online, and look up the terms lvalue and rvalue for more information.

Note that the function is returning the value of the pointer (I assume). It is not magically giving you access to the member of pre that stores that value. So even if you assign that value to a variable in LinkedList::Delete() and then change the value, that won't change the value that's stored in pre. For that, you most likely want to write an interface method for the Node class that allows calling code to modify that pointer - essentially, a setter method for that pointer.
Last edited on
Topic archived. No new replies allowed.