C++ : Expression must be a modifiable lvalue

标签 c++

我得到了

Expression must be a modifiable lvalue

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。这里有什么问题吗?

最佳答案

如错误所示,要编译:

 rear->getNextNode() = &node;

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

 Node*& getNextNode()
      ^

关于C++ : Expression must be a modifiable lvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37272379/

相关文章:

c++ - 引用成员函数?

c++ - 在哪里放置 using 声明

c++ - 从 NFD 到 NFC 的 OSX 和 C++ unicode 转换

c++ - 如何根据耗时制作 if 语句?

c++ - CMake 可以使用 c++ 而不是 mpicxx 来编译我的代码吗?

c++ - 在 Eclipse 中创建 .ipp 文件

c++ - 立即函数作为 Clang++ 中的默认函数参数初始值设定项

c++ - 在 C++ 类中声明多维数组的正确方法是什么

c++ - 在两个任务之间使用互斥信号量

c++ - 在 C++ 中导出没有基类的父类