0

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);

}


}
  • 1
    Do you understand what `const` at the end of the method signature means? – tkausl Feb 03 '19 at 03:05
  • `operator +` should be returning a brand new `Point` object, not a reference to the original. The same thing with `operator -`. If you want to return a reference to the original, that is what `operator +=` and `operator -=` are supposed to do. – PaulMcKenzie Feb 03 '19 at 03:07
  • Unrelated: [the great tome of wisdom](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) with respect to Operator Overloading. – user4581301 Feb 03 '19 at 03:30

1 Answers1

1

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @playpro0 Suggestion: Write less code, a function at the most, before compiling and testing. You find errors faster and are less likely to repeat it. In addition, one bug is usually easy to find and fix, but two bugs can feed off and hide one another, making the hunt many times harder. Don't give them time to build up. – user4581301 Feb 03 '19 at 03:26
  • Ok i see. Btw, is my code for the ++overloading correct? In terms of both the postfix and prefix. Just wanna double check. –  Feb 03 '19 at 03:26
  • Unrelated: in C++ `^` is the XOR operator. Using it for exponentiation can lead to confusion. – user4581301 Feb 03 '19 at 03:27