"lvalue required as left operand of assignment" 编写链表时出错
"lvalue required as left operand of assignment" error writing a linked list
我目前正在为我在学校上的课程学习一些 C++。我对左值和右值有基本的了解,但我无法确定为什么会收到编译器错误。
我正在创建一个单链表并且需要能够反转它。根据我的任务,我有两个 classes。第一个是节点,它只包含一个整数和一个指针。
class Node {
int data;
Node *next;
public:
//Constructor
Node(int d) {
data = d;
next = NULL;}
//Set to next Node
void SetNext(Node *nextOne) {
next = nextOne;}
//Returns data value
int Data(){return data;}
//Returns next Node
Node *Next() {return next;}
};
然后我有一个链表 class,它有一个 header 指针,然后是一些用于添加、打印等列表的函数。
class LinkedList {
Node *head;
public:
//Constructor
LinkedList(){head = NULL;}
void AddNode(int d) {
//Create a new Node
Node *newNode = new Node(d);
//Create a temporary pointer
Node *temp = head;
//If there are already nodes in the list
if(temp != NULL) {
//Parse through to the end of the list
while(temp->Next() != NULL) {
temp = temp->Next();}
//Point the last Node in the list to the new Node
temp->SetNext(newNode);
}
//If adding as the first Node
else{
head = newNode;}
}
void PrintList() {
//Temporary pointer
Node *temp = head;
//If there are no nodes in the list
if(temp == NULL) {
std::cout << "The list is empty" << std::endl;}
//If there is only one node in the list
if(temp->Next() == NULL) {
std::cout << temp->Data() << std::endl;}
//Parse through the list and print
else {
do {
std::cout << temp->Data();
temp = temp->Next();
}
while(temp != NULL);
}
}
//Returns the number of nodes in the list
int CountList() {
//Temporary pointer
Node *temp = head;
//Counter variable
int counter = 0;
//If the list is empty
if(temp == NULL) {
return counter;}
//Parse through Nodes counting them
else {
do {counter++;
temp = temp->Next();
}
while(temp != NULL);
}
return counter;
}
//Reverses the list
Node *ReverseList() {
//Initially set to NULL then tracks the new head
Node *marker = NULL;
//Tracks the next one in the list
Node *nextOne;
//Sets the first Node to NULL and then sets the last Node to point to
//the first one and rotates through the list pointing the last to the
//first
while(head != NULL) {
nextOne = head->Next();
head->Next() = marker;
marker = head;
head = nextOne;
}
//Setting the head back to the start again
head = marker;
}
};
其中一个函数应该反转列表。 ReverseList 函数中的 "head->Next() = marker;" 行在编译时导致 "lvalue required as left operand of assignment" 错误。
关于为什么会发生这种情况以及如何解决问题的任何见解?
提前致谢!
你的 Next() 函数 returns 一个指针,然后你这样做:
head->Next() = marker;
您正在更改指向标记的指针,而不是它指向的内容。要解决这个问题,您需要取消引用该指针:
*head->Next() = marker;
调用 Next()
的 return 是一个右值。由于你在class函数中,你不需要调用Next
函数来获取私有next
指针,你可以直接使用它。
head->next = marker;
您的下一个签名是:
Node *Next() {return next;}
这会在 return 复制下一个指针,因此它被视为右值而不是左值。
克服这个问题的一种方法是使用指向指针的指针:.
Node **Next() {return &next;}
然后将其用作:
int main()
{
Node* marker=new Node(89);
Node* nod=new Node(9);
*(nod->Next())= marker;
cout<<(nod->next)->data<<endl;
cout << "Hello World" << endl;
return 0;
}
这使得使用起来更加复杂。
我目前正在为我在学校上的课程学习一些 C++。我对左值和右值有基本的了解,但我无法确定为什么会收到编译器错误。
我正在创建一个单链表并且需要能够反转它。根据我的任务,我有两个 classes。第一个是节点,它只包含一个整数和一个指针。
class Node {
int data;
Node *next;
public:
//Constructor
Node(int d) {
data = d;
next = NULL;}
//Set to next Node
void SetNext(Node *nextOne) {
next = nextOne;}
//Returns data value
int Data(){return data;}
//Returns next Node
Node *Next() {return next;}
};
然后我有一个链表 class,它有一个 header 指针,然后是一些用于添加、打印等列表的函数。
class LinkedList {
Node *head;
public:
//Constructor
LinkedList(){head = NULL;}
void AddNode(int d) {
//Create a new Node
Node *newNode = new Node(d);
//Create a temporary pointer
Node *temp = head;
//If there are already nodes in the list
if(temp != NULL) {
//Parse through to the end of the list
while(temp->Next() != NULL) {
temp = temp->Next();}
//Point the last Node in the list to the new Node
temp->SetNext(newNode);
}
//If adding as the first Node
else{
head = newNode;}
}
void PrintList() {
//Temporary pointer
Node *temp = head;
//If there are no nodes in the list
if(temp == NULL) {
std::cout << "The list is empty" << std::endl;}
//If there is only one node in the list
if(temp->Next() == NULL) {
std::cout << temp->Data() << std::endl;}
//Parse through the list and print
else {
do {
std::cout << temp->Data();
temp = temp->Next();
}
while(temp != NULL);
}
}
//Returns the number of nodes in the list
int CountList() {
//Temporary pointer
Node *temp = head;
//Counter variable
int counter = 0;
//If the list is empty
if(temp == NULL) {
return counter;}
//Parse through Nodes counting them
else {
do {counter++;
temp = temp->Next();
}
while(temp != NULL);
}
return counter;
}
//Reverses the list
Node *ReverseList() {
//Initially set to NULL then tracks the new head
Node *marker = NULL;
//Tracks the next one in the list
Node *nextOne;
//Sets the first Node to NULL and then sets the last Node to point to
//the first one and rotates through the list pointing the last to the
//first
while(head != NULL) {
nextOne = head->Next();
head->Next() = marker;
marker = head;
head = nextOne;
}
//Setting the head back to the start again
head = marker;
}
};
其中一个函数应该反转列表。 ReverseList 函数中的 "head->Next() = marker;" 行在编译时导致 "lvalue required as left operand of assignment" 错误。
关于为什么会发生这种情况以及如何解决问题的任何见解?
提前致谢!
你的 Next() 函数 returns 一个指针,然后你这样做:
head->Next() = marker;
您正在更改指向标记的指针,而不是它指向的内容。要解决这个问题,您需要取消引用该指针:
*head->Next() = marker;
调用 Next()
的 return 是一个右值。由于你在class函数中,你不需要调用Next
函数来获取私有next
指针,你可以直接使用它。
head->next = marker;
您的下一个签名是:
Node *Next() {return next;}
这会在 return 复制下一个指针,因此它被视为右值而不是左值。
克服这个问题的一种方法是使用指向指针的指针:.
Node **Next() {return &next;}
然后将其用作:
int main()
{
Node* marker=new Node(89);
Node* nod=new Node(9);
*(nod->Next())= marker;
cout<<(nod->next)->data<<endl;
cout << "Hello World" << endl;
return 0;
}
这使得使用起来更加复杂。