gpt4 book ai didi

C++ : Expression must be a modifiable lvalue

转载 作者:行者123 更新时间:2023-11-30 05:27:30 24 4
gpt4 key购买 nike

我得到了

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com