user10976738
user10976738

Reputation:

Overloading operators with compilation error

Hi i am having a compilation error in my codes shown below. I really do not know why that's why i am here. Can anyone help me with correcting my errors? New to operator overloading. Thanks in advance.

This is the compilation error that i am receiving:

//Point.cpp:45:11: error: assignment of member ‘CS170::Point::x’ in read-only object

x+=other.x; (.x is highlighted)

//Point.cpp:117:12: error: assignment of member ‘CS170::Point::y’ in read-only object

y=y-other.y; (.y is highlighted)

//Point.cpp:47:9: error: binding reference of type ‘CS170::Point&’ to ‘const CS170::Point’ discards qualifiers

return *this;(*this being highlighted)

//Point.cpp:58:13: error: cannot bind non-const lvalue reference of type ‘CS170::Point&’ to an rvalue of type ‘CS170::Point’

  return Point(pow(x,other.x),pow(y,other.y)); 

(Point(pow(x.other.x),power(y,other.y) is highlighted)

Point.cpp: In member function ‘CS170::Point& CS170::Point::operator%(double)’: Point.cpp:94:5: error: invalid operands of types ‘double’ and ‘double’ to binary ‘operator%’

x=x%value;(x%value being highlighted)

//Point.cpp:143:41: error: no ‘int CS170::Point::Multiply(const CS170::Point&)’ member function declared in class ‘CS170::Point’

int Point::Multiply(const Point& other)

//Point.cpp:76:31: error: unused parameter ‘dummy’ [-Werror=unused-parameter]

Point& Point::operator--(int dummy) ( dummy is highlighted)

#include "Point.h"  

#include <cmath>    

namespace CS170

{

    const double PI = 3.1415926535897;

    const double EPSILON = 0.00001;

/////////////////////////////////////////////////////////////////////////////// // private member functions

double Point::DegreesToRadians(double degrees) const
{;

    return (degrees * PI / 180.0);

}

double Point::RadiansToDegrees(double radians) const
{

    return (radians * 180.0 / PI);

}

/////////////////////////////////////////////////////////////////////////////// // 16 public member functions (2 constructors, 14 operators)

Point::Point(double X, double Y): x(X), y(Y) { }


Point::Point(){

    x=0.0f;

    y=0.0f;

}
Point& Point::operator+(double value)
{

    x=x+value;

    y=y+value;

    return *this;

}
Point& Point::operator+(const Point& other) const
{

    x+=other.x; 

    y+=other.y;

    return *this;

}

Point& Point::operator-(const Point& other)
{

    return -(other.x), -(other.y) ;

}
Point& Point::operator^(const Point& other) 
{

    return Point(pow(x,other.x),pow(y,other.y));

}
Point& Point::operator++(int dummy)
{

    x++;

    y++;

    return *this;

}

Point& Point::operator++()
{

    ++x;

    ++y;

    return *this;

}

Point& Point::operator--(int dummy)
{

    x--;

    y--;

    return *this;

}

Point& Point::operator--()
{
    --x;

    --y;

    return *this;

}

Point& Point::operator%(double value)
{

    x=x%value;

    y=y%value;

    return *this;

}

Point& Point::operator+=(const Point& other) const
{

    x += other.x;

    y += other.y;

    return *this;

}

Point& Point::operator+=(double value)
{

    return Point(x+value,y+value);

}
Point& Point::operator-(int value)
{

    return Point(x-value,y-value);

}

Point& Point::operator-(const Point& other) const

{

    x=x-other.x;

    y=y-other.y;

// return Point(x-other.x,y-other.y);
    return *this;

}

Point& Point::operator*(const Point& other) const
{

    return Multiply(other);

}

/////////////////////////////////////////////////////////////////////////////// // 2 friend functions (operators)

std::ostream& operator<< (std::ostream &output, const Point &point)
    { 

        output << "(" << point.x << "," << point.y << ")";

        return output;

    }   

std::istream& operator>>(std::istream &input, Point &point ) 
    { 

        input >> point.x >> point.y;

        return input;            

    }

/////////////////////////////////////////////////////////////////////////////// // 2 non-members, non-friends (operators)

int Point::Multiply(const Point& other) 
{

    return Point(x*other.x, y*other.y);

}
int Point::Add(const Point& other) 
{           

    return Point(x+other.x, y+other.y);

}


}

Upvotes: 0

Views: 289

Answers (1)

R Sahu
R Sahu

Reputation: 206577

Problem 1

You need to make the operator+= member function a non-const member function.

Point& Point::operator+=(const Point& other)  // No const
{
    x += other.x;

    y += other.y;

    return *this;    
}

The function modifies the object on which the function is called. It does not make sense to make it a const member function.

Problem 2

  • The operator+ function needs return an object, not a reference to an object.
  • Its implementation needs to be changed so that it does modify the current object.
  • The implementation can be simplified by using the += operator.

Here's an updated version.

Point Point::operator+(const Point& other) const
{
    Point ret(*this);
    return (ret += other);
}

Problem 3

The operator^ function needs to return an object, not a reference.

Point Point::operator^(const Point& other) 
{
    return Point(pow(x,other.x),pow(y,other.y));
}

When you use return Point(...);, it cannot be a reference to an object.

Upvotes: 1

Related Questions