编写链表时"lvalue required as left operand of assignment"错误



我目前正在为学校的一门课程学习一些c++。我对左值和右值有基本的了解,但我无法确定为什么我收到编译器错误。

我正在创建一个单链表,需要能够逆转它。根据我的作业,我有两节课。第一个是节点,只保存一个int和一个指针。

 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 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;"行在编译时导致"左值要求作为赋值的左操作数"错误。

关于为什么会发生这种情况以及我如何纠正这个问题,有什么见解吗?

提前感谢!

调用Next()的返回值为右值。因为你是在一个类函数中,你不需要调用Next函数来获取私有的next指针,你可以直接使用它。

head->next = marker;

你的Next()函数返回一个指针,然后你这样做:

head->Next() = marker;

你将指针更改为marker,而不是它所指向的对象。要解决这个问题,需要对该指针解引用:

*head->Next() = marker;

你的下一个签名是:

Node *Next() {return next;}

这会在返回时复制next指针,因此它被视为r值而不是l值。

解决这个问题的一种方法是使用指针对指针:。

 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;
  }

相关内容

最新更新