0

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;
                    }
                }
            }
        }
};
mkz
  • 119
  • 1
  • 8
  • 3
    "Every time i run the code i get this lvalue error", unlikely. The kind of errors which could be described as "lvalue error" are compiler errors, which means you cannot run the code. Please give more details about the error you get. In case of compiler errors quote it directly here, completely, verbatim and as text. In case of an error which actually occurs while running the result of compilation etc. please give a much more detailed description. – Yunnosch Jun 15 '20 at 16:24
  • Please provide a [mre] of the code you try to compile or run. – Yunnosch Jun 15 '20 at 16:25
  • Helpful reading: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – user4581301 Jun 15 '20 at 16:29
  • Next time you post, please please please indicate _where the error is_! – TonyK Jun 15 '20 at 18:07

3 Answers3

1

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;
}
asmmo
  • 6,922
  • 1
  • 11
  • 25
  • Far better just to admit to yourself that you are circumventing encapsulation, and make the member public. – Bathsheba Jun 15 '20 at 16:37
0

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

samuelnj
  • 1,627
  • 1
  • 10
  • 19
0

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.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141