I am having an error when I try to compile my code. The code is overloading operators and all of the overloaded operators have worked until I tried to assign the copy constructor. I keep getting a "MyClass operator=(const MyClass&)’ must be a nonstatic member function" error. I don't understand why overloading the "=" operator would cause such an error. Putting the "friend" special word in front of the declaration in the .h file does not fix the issue.
main.cpp
#include <iostream>
#include "Point.h"
using namespace std;
int main() {
Point point1( 2, 5);
Point point2 = point1;
return 0;
}
MyClass.h
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
#include <cmath>
using namespace std;
class Point {
public:
//Constructor
Point();
Point(const double x, const double y);
//Copy
Point(const Point & t);
//Destructor
virtual ~Point();
//Get the x value
double getX() const;
//Get the y value
double getY() const;
//Set the x value
void setX(double x);
//Set the y value
void setY(double y);
//Return the distance between Points
double distance(const Point& p) const;
//Output the Point as (x, y) to an output stream
friend ostream& operator << (ostream& out, const Point& point);
//Comparison relationships
friend bool operator == (const Point& lhs, const Point& rhs);
friend bool operator < (const Point& lhs, const Point& rhs);
//Math operators
friend Point operator + (const Point& lhs, const Point& rhs);
friend Point operator - (const Point& lhs, const Point& rhs);
Point& operator = (const Point& rhs);
private:
double x;
double y;
};
#endif /* POINT_H_ */
MyClass.cpp
// Get the x value
double Point::getX() const {
return x;
}
// Get the y value
double Point::getY() const {
return y;
}
void Point::setX(double x) {
this->x = x;
}
void Point::setY(double y) {
this->y = y;
}
// Return the distance between Points
double Point::distance(const Point& p) const{
return abs( sqrt( pow( (x - p.getX() ), 2 ) + pow( (y - p.getY() ), 2 ) ) );
}
ostream& operator << (ostream& out, const Point& point){
out << point.getX() << ", " << point.getY();
return out;
}
bool operator == (const Point& lhs, const Point& rhs){
if(lhs.x == rhs.x && lhs.y == rhs.y){ return true; }
return false;
}
bool operator < (const Point& lhs, const Point& rhs){
if(lhs.x < rhs.x && lhs.y < rhs.y){ return true; }
return false;
}
Point operator + (const Point& lhs, const Point& rhs){
Point point;
point.x = lhs.x + rhs.x;
point.y = lhs.y + rhs.y;
return point;
}
Point operator - (const Point& lhs, const Point& rhs){
Point point;
point.x = lhs.x - rhs.x;
point.y = lhs.y - rhs.y;
return point;
}
Point& Point::operator = (const Point& rhs){
x = rhs.x;
y = rhs.y;
return *this;
}
// Destructor
Point::~Point(){}