vendredi 1 juillet 2022

C++ error: using temporary as lvalue when using member variable of container in dervied class

#include<iostream>

class acc{
    public:
        acc() { aa = 10;}
        int aa;
};
class Derv;
class Base{
    public:
       Base() {}
       int a;
       virtual acc get_acc() {};
       virtual void compare(Derv *v) {}


};

class Derv : public Base {
    public:
        acc acc_o;
        Derv() {}
        virtual acc get_acc();
        virtual void compare(Derv *v) {
            if(v->get_acc().aa = acc_o.aa) {
                std::cout<<"Values Match\n";
            }
        }
};

acc Derv::get_acc() {
    return acc_o;
}


int main() {
    Base *d1 = new Derv();
    Base *v = new Derv();
    Derv *v1 = dynamic_cast<Derv *>(v);

    d1->compare(v1);

    std::cout<<d1->get_acc().aa<<std::endl;
    return 0;
}

Above code is giving compiler error when compiled using gcc. Following is the compiler error:

accessor_test.cpp: In member function ‘virtual void Derv::compare(Derv*)’:
accessor_test.cpp:25:40: error: using temporary as lvalue [-fpermissive]
             if(v->get_acc().aa = acc_o.aa) {

Not able to understand what is the reason for error? Can someone help me understand the error and possible fix for the same?

Aucun commentaire:

Enregistrer un commentaire