1

I got

Expression must be a modifiable lvalue

in

rear->getNextNode() = &node;

Here is code:

using namespace std;
#include<iostream>
#include<string>


class Node
{

    string name;
    Node* next;
    int arrivedTime;
    int runningTime;
    char state='R';

public:

    Node(char* name,int arrivedTime,int runningTime):name(name),arrivedTime(arrivedTime),runningTime(runningTime){}

    void printState()
    {
        cout << "name=" << name << " " << endl;
    }

    void execute()
    {
        runningTime--;
        printState();
    }

    bool whetherArrive()
    {
        if (arrivedTime > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    void downArrivedTime()
    {
        arrivedTime--;
    }

    Node* getNextNode()
    {
        return next;
    }
};

class Queue
{
public:
    Node* head;
    Node* rear;
    Node* p;


    void insert(Node &node)
    {
        if (head == NULL)
        {
            head = &node;
            rear = &node;
            p = &node;

        }
        else
        {

            rear->getNextNode() = &node; //here hint Expression must be a modifiable lvalue
        }
    }

};

int main()
{
    cout << "input: process-count" << endl;
    int processCount;
    cin >> processCount;
    for (int i = 0; i < processCount; i++)
    {
        cout << "input:process-name arrivedTime runningTime" << endl;
        char name[20];
        int arrivedTime;
        int runningTime;
        cin >> name >> arrivedTime >> runningTime;
        Node node(name, arrivedTime, runningTime);
    }
}

rear->getNextNode() return a pointer to Node, and then set the point &node. What's wrong here?

guo
  • 9,674
  • 9
  • 41
  • 79

1 Answers1

1

As in the error, to make this compile:

 rear->getNextNode() = &node;

getNextNode() must return lvalue so you need to modify singature to:

 Node*& getNextNode()
      ^
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • What does `*&` mean? – guo May 17 '16 at 09:44
  • 2
    @guo thats reference to pointer of type Node, but I would recomend to add `setNextNode` method instead. – marcinj May 17 '16 at 09:51
  • I am still confused about something.. Without `&`, it return a copy of the pointer, which is, of course, modifiable. But why not IDE says it is modifiable? – guo May 17 '16 at 09:53
  • @guo it returns a value (in case of pointer an address) which is stored inside next. The same story is with primitive types. If instead of pointer you would have a type like `int`, you would have `int getValue() { return int_variable; }` you cant assign to it using `getValue() = 123;`, as getValue returns rvalue. – marcinj May 17 '16 at 09:57
  • And what will return if using `*&` ? As I see it, it still return a value (in case of pointer an address) which is stored inside next, except that the returned thing is the pointer I owned before. – guo May 17 '16 at 10:14
  • `*&` returns a reference - or an alias to your next variable. This way `rear->getNextNode() = &node;` is equivalent to `read->next = &node;` – marcinj May 17 '16 at 10:22