需要左值作为赋值的左操作数??在链表中添加Add_End、删除和Delete_Front?
lvalue required as left operand of assignment?? add Add_End, Delete, and Delete_Front in a linked list?
我看过很多 youtube 教程,但他们都说同样的话。有没有其他方法 link 节点不同,而不是“prev->Next()=temp -> Next();” ?每次我使用它时,它都会显示相同的错误,即左值需要作为赋值的左操作数?请帮忙!这是代码。
#include <iostream>
using namespace std;
class Node {
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
private:
int data;
Node* next;
};
class List {
public:
List() {
head = NULL;
}
void Add_End(int data);
void Delete(int data);
void Delete_Front();
void Add_Front(int data);
void Delete_End();
Node* Find(int data);
void Print();
private:
Node *head;
};
void List::Add_End(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
if(head == NULL) {
head = newNode;
return;
}
Node *temp = head;
while(temp->Next()!= NULL)
{
temp=temp->Next();
}
temp->SetNext(newNode);
return;
}
void List::Delete(int data) {
Node *prev = head;
int i;
if(data == 1){
head = prev->Next();
delete prev;
return;
}
for(i = 0; i <(data-2);i=i+1){
prev = prev->Next();
}
Node *temp = prev->Next();
prev->Next()=temp -> Next();
delete temp;
return;
}
void List::Delete_Front() {
if(head == NULL) {
cout<<"List has no member so cannot delete front"<<endl;
return;
}
delete head;
head = head-> Next();
return;
}
void List::Add_Front(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
return;
}
void List::Delete_End() {
if(head == NULL) {
cout<<"List has no member so cannot delete end"<<endl;
return;
}
if(head->Next() == NULL) {
delete head;
head = NULL;
return;
}
Node *current;
Node *prev;
prev = head;
for(current = head->Next(); current->Next() != NULL; current = current- >Next()) {
prev = current;
}
prev->SetNext(NULL);
delete current;
return;
}
Node* List::Find(int data) {
Node *current;
for(current = head; current!= NULL && current->Data() != data; current = current->Next())
{}
if(current == NULL) {
cout<<"Did not find "<<data<<"."<<endl;
return NULL;
}
else {
cout<<"Found "<<data<<"."<<endl;
return current;
}
}
void List::Print() {
Node *current;
cout<<"Linked List Nodes: "<<endl;
if(head == NULL){
cout<<"List is empty." <<endl;
}
for(current = head; current != NULL; current = current->Next()){
cout<<current->Data();
cout<<endl;
}
return;
}
int main(){
List list;
Node *answer;
list.Add_End(111);
list.Print();
list.Add_End(222);
list.Print();
list.Add_End(333);
list.Print();
list.Add_End(444);
list.Print();
list.Add_End(555);
list.Print();
list.Delete(444);
list.Print();
list.Delete(333);
list.Print();
list.Delete(222);
list.Print();
list.Delete(555);
list.Print();
list.Delete(111);
list.Print();
list.Add_End(23);
list.Print();
list.Add_End(45);
list.Print();
list.Add_End(26);
list.Print();
list.Delete_Front();
list.Print();
cout<<"Testing Add_Front: and others"<<endl;
list.Add_Front(888);
list.Print();
list.Add_Front(999);
list.Print();
list.Add_Front(49);
list.Print();
cout<<"Checking find function"<<endl;
answer = list.Find(888);
cout<<"Value for node returned by find function call with 888 is "
<<answer->Data()<<"."<<endl;
cout<<"Checking find function"<<endl;
answer = list.Find(999);
cout<<"Value for node returned by find function call with 999 is "
<数据()<<"."<
cout<<"Checking find function"<<endl;
answer = list.Find(49);
cout<<"Value for node returned by find function call with 49 is "<<answer-
>Data()<<"."<<endl;
cout<<"Call find function with value not in list."<<endl;
answer = list.Find(7);
if(answer == NULL) {
cout<<"returned null pointer since 7 not found"<<endl;
}
else{
cout<< "in else of answer == NULL where Value for node returned by
find function call with 7 is "<<answer->Data()<<"."<<endl;
}
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
return 0;
}
这里的问题是Node的Next()
方法returns复制了next
成员变量,因此变成了r-value(const temporary object)不能在赋值中用作左值。
这就是它抛出错误 lvalue required as left operand of assignment
的原因。
要实现该目的,您可以使用 Node
class 的 SetNext()
方法,如下所示:
prev->SetNext(temp->Next());
我希望这会解决您的问题。
谢谢
我看过很多 youtube 教程,但他们都说同样的话。有没有其他方法 link 节点不同,而不是“prev->Next()=temp -> Next();” ?每次我使用它时,它都会显示相同的错误,即左值需要作为赋值的左操作数?请帮忙!这是代码。
#include <iostream>
using namespace std;
class Node {
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
private:
int data;
Node* next;
};
class List {
public:
List() {
head = NULL;
}
void Add_End(int data);
void Delete(int data);
void Delete_Front();
void Add_Front(int data);
void Delete_End();
Node* Find(int data);
void Print();
private:
Node *head;
};
void List::Add_End(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
if(head == NULL) {
head = newNode;
return;
}
Node *temp = head;
while(temp->Next()!= NULL)
{
temp=temp->Next();
}
temp->SetNext(newNode);
return;
}
void List::Delete(int data) {
Node *prev = head;
int i;
if(data == 1){
head = prev->Next();
delete prev;
return;
}
for(i = 0; i <(data-2);i=i+1){
prev = prev->Next();
}
Node *temp = prev->Next();
prev->Next()=temp -> Next();
delete temp;
return;
}
void List::Delete_Front() {
if(head == NULL) {
cout<<"List has no member so cannot delete front"<<endl;
return;
}
delete head;
head = head-> Next();
return;
}
void List::Add_Front(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
return;
}
void List::Delete_End() {
if(head == NULL) {
cout<<"List has no member so cannot delete end"<<endl;
return;
}
if(head->Next() == NULL) {
delete head;
head = NULL;
return;
}
Node *current;
Node *prev;
prev = head;
for(current = head->Next(); current->Next() != NULL; current = current- >Next()) {
prev = current;
}
prev->SetNext(NULL);
delete current;
return;
}
Node* List::Find(int data) {
Node *current;
for(current = head; current!= NULL && current->Data() != data; current = current->Next())
{}
if(current == NULL) {
cout<<"Did not find "<<data<<"."<<endl;
return NULL;
}
else {
cout<<"Found "<<data<<"."<<endl;
return current;
}
}
void List::Print() {
Node *current;
cout<<"Linked List Nodes: "<<endl;
if(head == NULL){
cout<<"List is empty." <<endl;
}
for(current = head; current != NULL; current = current->Next()){
cout<<current->Data();
cout<<endl;
}
return;
}
int main(){
List list;
Node *answer;
list.Add_End(111);
list.Print();
list.Add_End(222);
list.Print();
list.Add_End(333);
list.Print();
list.Add_End(444);
list.Print();
list.Add_End(555);
list.Print();
list.Delete(444);
list.Print();
list.Delete(333);
list.Print();
list.Delete(222);
list.Print();
list.Delete(555);
list.Print();
list.Delete(111);
list.Print();
list.Add_End(23);
list.Print();
list.Add_End(45);
list.Print();
list.Add_End(26);
list.Print();
list.Delete_Front();
list.Print();
cout<<"Testing Add_Front: and others"<<endl;
list.Add_Front(888);
list.Print();
list.Add_Front(999);
list.Print();
list.Add_Front(49);
list.Print();
cout<<"Checking find function"<<endl;
answer = list.Find(888);
cout<<"Value for node returned by find function call with 888 is "
<<answer->Data()<<"."<<endl;
cout<<"Checking find function"<<endl;
answer = list.Find(999);
cout<<"Value for node returned by find function call with 999 is "
<数据()<<"."<
cout<<"Checking find function"<<endl;
answer = list.Find(49);
cout<<"Value for node returned by find function call with 49 is "<<answer-
>Data()<<"."<<endl;
cout<<"Call find function with value not in list."<<endl;
answer = list.Find(7);
if(answer == NULL) {
cout<<"returned null pointer since 7 not found"<<endl;
}
else{
cout<< "in else of answer == NULL where Value for node returned by
find function call with 7 is "<<answer->Data()<<"."<<endl;
}
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
return 0;
}
这里的问题是Node的Next()
方法returns复制了next
成员变量,因此变成了r-value(const temporary object)不能在赋值中用作左值。
这就是它抛出错误 lvalue required as left operand of assignment
的原因。
要实现该目的,您可以使用 Node
class 的 SetNext()
方法,如下所示:
prev->SetNext(temp->Next());
我希望这会解决您的问题。
谢谢