简体   繁体   中英

I want to bubble sort a singly linked list in c++, but i keep facing a lvalue error and i am unable to resolve it

Here is the code that might help you understand my problem, i am using a node class and a list class. Here is the error: [Error] lvalue required as left operand of assignment. any help would be appreciated..!!

class Node{
    private:
        int data;
        Node* next;
    public:
        void SetNext(Node* next){
            this->next=next;
        }
        void SetData(int data){
            this->data=data;
        }
        Node* GetNext(){
            return next;
        }
        int GetData(){
            return data;
        }
};
class List{
    private:
        Node *CurrentLocation;
    public:
        List(){
            CurrentLocation=NULL;
        }
    void SortList(){

            Node *t1,*t2;
            int temp;
            for(t1=CurrentLocation;t1!=NULL;t1=t1->GetNext()){
                for(t2=CurrentLocation;t2!=NULL;t2=t2->GetNext()){
                    if(t2->GetData()>t1->GetNext()->GetData()){
                        temp=t2->GetData();
                        t2->GetData()=t2->GetNext()->GetData();
                        t2->GetNext()->GetData()=temp;
                    }
                }
            }
        }
};

You are trying to assign to an rvalue . This rvalue is a temporary copy of your data. not the original one. It will be destroyed as soon as the expression executes, hence u can't assign to it.

To assign to your data, u must return a reference to it, as follows

int& GetData(){
    return data;
}

As user4581301 has mentioned, I advise you to see this What are rvalues, lvalues, xvalues, glvalues, and prvalues?

The previous description was upon the problem itself but It would be better to write a setter for your data like your getter, as follows

void setData(int newData){
    data = newData;
}

Here's the problem line:

t2->GetData()=t2->GetNext()->GetData();

The issue is that the GetData() function returns a copy of the member data which has no local storage (you haven't assigned it to a named variable, hence it's an rvalue). The reason the compiler complains is because really this does nothing. You're assigning a new value to a temporary value that's going away immediately after the line executes.

Converting the member function to

int& GetData(){
    return data;
}

means when you call t2->GetData() it actually refers to the data member of t2 and it isn't just a temporary that goes away.

Really though it's probably just better to make data a public member variable so you can just set it/read from it directly

This line is problematic:

t2->GetData()=t2->GetNext()->GetData();

You're trying to assign a value into the data int. However, the GetData member function don't return data itself: is instead returns a temporary copy. You cannot assign into temporaries. To grossly simplify, the compiler call temporaries as rvalue , and non temporary as lvalue . Assignments need lvalues, as it would make little sense to assign into temporaries.

The best would be to use the setter function to set the value:

t2->SetData(t2->GetNext()->GetData());

Here the setter will assign into the data data member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM