2015-10-04 58 views
1

我目前正在學習一些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; 
    } 

}; 

其中一個函數應該顛倒列表。行「head-> Next()= marker;」在編譯時,ReverseList函數會導致「作爲賦值左操作數所需的左值」錯誤。

任何有關爲什麼會發生這種情況的洞察力以及我如何糾正問題?

預先感謝您!

+0

請使這[最小的,完整的和可驗證的例子](https://stackoverflow.com/help/mcve) –

回答

1

從調用Next()回報是右值。由於您處在班級功能中,因此您無需撥打Next功能即可獲得專用next指針,您可以直接使用它。

head->next = marker; 
0

你的下一個()函數返回一個指針,然後你這樣做:

head->Next() = marker; 

你改變指針來標記,不是它的指向。爲了解決這個問題,你需要取消引用該指針:

*head->Next() = marker; 
0

你的下個簽名是:

Node *Next() {return 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; 
    } 

這使得它更復雜的使用。

+0

雖然這修復了錯誤,它相當破壞封裝,OP也許需要重新設計他的類的接口 –

+0

封裝的想法是好的,但這也取決於他是否打算將其用作圖書館或其他方式。 – basav

相關問題