c ++:表达式必须是可修改的左值

时间:2016-05-17 09:34:34

标签: c++

我得到了

  

表达式必须是可修改的左值

rear->getNextNode() = &node;

这是代码:

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()返回指向Node的指针,然后设置点&node。这有什么不对?

1 个答案:

答案 0 :(得分:1)

在错误中,要进行编译:

 rear->getNextNode() = &node;

getNextNode()必须返回左值,因此您需要将singature修改为:

 Node*& getNextNode()
      ^