Search code examples
c++classoopreferenceconstants

initial value of reference to non-const must be an lvalue, Passing an object type by reference error


I get the following error when I try to pass a Point object by reference by keeping Point x and y member variables as private, which is why I get x and y by function GetX() and GetY() how do I resolve the error and get it to work as I expect it to.

error log :

CppReview.cpp: In function 'int main()':
CppReview.cpp:92:30: error: cannot bind non-const lvalue reference of type 'Point&' to an rvalue of type 'Point'
   92 |     v.OffSetVector(v.GetStart(),1,3);
      |                    ~~~~~~~~~~^~
CppReview.cpp:79:34: note:   initializing argument 1 of 'void Vector::OffSetVector(Point&, int, int)'
   79 | void Vector::OffSetVector(Point& p,int xoffset,int yoffset){

code :


class Point{
    private:
        int x,y;
    public:
        Point(){
            x = y = 0;
        }
        Point(int x,int y){
            this->x = x;
            this->y = y;
        }
        Point(float x,float y){
            this->x = x;
            this->y = y;
        }
        void SetX(int x){
            this->x = x;
        }
        
        void SetY(int y){
            this->y = y;
        }
        void Display(){
            cout<<"("<<this->x<<','<<this->y<<")\n";
        }
        void move(int i=0,int j=0){
            this->x+=i;
            this->y+=j;
        }

        int& GetX(){
            return (this->x);
        }

        int& GetY(){
            return this->y;
        }
};

class Vector{
    Point start,end;
    public:
    Vector(){
        this->start = Point(0,0);
        this->end = Point(1,1);
    }
    Vector(int sx,int sy,int ex,int ey){
        this->start = Point(sx,sy);
        this->end = Point(ex,ey);
    }
    float ComputeDistance(Point,Point);
    Point GetStart();
    Point GetEnd();
    void OffSetVector(Point&,int,int);
    void Show();
};

float Vector::ComputeDistance(Point p1,Point p2){
    int p1x = p1.GetX();
    int p1y = p1.GetY();
    int p2x = p2.GetX();
    int p2y = p2.GetY();
    float dist = sqrt((p1x-p2x)*(p1x-p2x)+(p1y-p2y)*(p1y-p2y));
    return dist;
}

Point Vector::GetStart(){
    return this->start;
}

Point Vector::GetEnd(){
    return this->end;
}

void Vector::OffSetVector(Point& p,int xoffset,int yoffset){
    p.GetX()+=xoffset;
    p.GetY()+=yoffset;
}


void Vector::Show(){
    cout<<this->GetStart().GetX()<<','<<this->GetStart().GetY()<<" : "<<this->GetEnd().GetX()<<','<<this->GetEnd().GetY()<<"\n";
}



int main(){
    Vector v(1,1,3,3);
    v.Show();
    v.OffSetVector(v.GetStart(),1,3);
    return 0;
}


Solution

  • The function GetStart returns a temporary object of the type Point:

    Point GetStart();
    

    while the function OffsetVector excepts a non-constant reference to an object:

    void OffSetVector(Point&,int,int);
    

    You may not bind a temporary object with a non-constant lvalue reference.

    Change the declaration of the function GetStart like:

    Point & GetStart();
    

    Also at least the function GetEnd should be changed like:

    Point & GetEnd();
    

    You could overload the functions for constant and non-constant objects:

    Point & GetSgtart();
    cosnt Point & GetStart() const;
    Point & GetEnd();
    const Point & GetEnd() const;