Weird behaviour in overloading operator+ (Xcode vs .Net)
Hi there,
I just started with programming in C++ with a video tutorial. Everything was going alright until i got to a weird compiler error while overloading the operator+ for a class I created myself (See code below).
Since the tutorial was done in .NET and I am working with Xcode I tried the same code (copy pasted it) in .NET myself and got no compiler errors, every thing seemed fine.
I think this is pretty strange since it's just basic C++ without having used any platform dependent code. Has anybody got any idea what it can be or how it can be solved?
Here is the code (the error and location are after the code):
Code:
#include <iostream>
using namespace std;
//------------------------------
//Point class begin
//------------------------------
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();
float getX();
float getY();
float getZ();
void setX(float X);
void setY(float Y);
void setZ(float Z);
Point operator =(Point &p);
Point operator +(Point &p);
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
x = f_x;
y = f_y;
z = f_z;
}
Point::~Point()
{
}
float Point::getX()
{
return x;
}
float Point::getY()
{
return y;
}
float Point::getZ()
{
return z;
}
void Point::setX(float X)
{
x = X;
}
void Point::setY(float Y)
{
y = Y;
}
void Point::setZ(float Z)
{
z = Z;
}
Point Point::operator =(Point &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());
return *this;
}
Point Point::operator +(Point &p)
{
Point outP;
outP.setX(getX() + p.getX());
outP.setY(getY() + p.getY());
outP.setZ(getZ() + p.getZ());
return outP;
}
//---------------------------
//Point class end
//---------------------------
int main (int argc, char * const argv[])
{
Point p1(4, 2, 1);
Point p2(6, 3, 8);
Point p3;
p3 = p2 + p1;
return 0;
}
When I compile this in Xcode I get the follow error on the following line in main:
error: no match for 'operator=' in 'p3 = Point::operator+(Point&)(((Point&)(& p1)))'
as I said, if I copy paste this code to .NET (Visual C++ 2008 Express edition) it works perfectly with no errors;
I also found out that if I write the following statement:
Code:
Point p3 = p2 + p1;
the compiler doesn't give an error in Xcode and it can be run correctly with normal behaviour.
Very strange if I may say so, but maybe it's because I'm new to C++ and I missed a detail.