Is it possible to overload one operator more than once? I have a struct that is just my idea of a C++ Box. I'm trying to increase the size of a box using an integer or another box.
I'm trying to do:
Box X(0, 0, 0, 0);
Box Y(1, 1, 1, 1);
X + 3; //X should now be (0, 0, 3, 3);
X + Y; //X should now be (1, 1, 4, 4);
But I get these errors because the operator isn't defined. But I already defined the + operator for integers yet it says it's not defined. I think I'm mis-using the friend keyword. Should my == operator be friend? The error I get is:undefined reference to operator+(Box, int)'|
I defined it at the very last 13 lines of the implementation file.
Header File:
struct Box
{
int X1, Y1, X2, Y2, W, H;
Box(); //Default Constructor.
Box(int X1_, int Y1_, int X2_, int Y2_); //Alternate Constructor.
Box(Point UpperLeft, Point LowerRight);
~Box(){}; //Destructor.
Box& operator ()(int x1, int y1, int x2, int y2);
Box& operator ()(RECT B);
friend bool operator == (const Box &B, const Box &B2);
friend inline bool operator != (const Box &B, const Box &B2);
Box operator += (Box B);
Box operator -= (Box B);
Point MidPointBox (const Box &B);
Box& PointToBox(Point UpperLeft, Point LowerRight);
Box& operator = (const Box& B);
bool Contains(Point P) const;
bool Contains(const Box& B) const;
friend ostream& operator << (ostream& Str, const Box &B);
friend inline Box operator + (Box B1, int S);
friend inline Box operator - (Box B1, int S);
};
My Implementation File:
/**
* DECLARATION FOR BOXES
*
*
**/
Box::Box() : X1(0), Y1(0), X2(0), Y2(0){W = 0; H = 0;}
Box::Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_)
{
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2); H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
}
Box::Box(Point UpperLeft, Point LowerRight) : X1(UpperLeft.X), Y1(UpperLeft.Y), X2(LowerRight.X), Y2(LowerRight.Y)
{
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2); H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
}
Box& Box::operator ()(int x1, int y1, int x2, int y2)
{
if ((X1 != x1) && (Y1 != y1) && (X2 != x2) && (Y2 != y2))
{
X1 = x1;
Y1 = y1;
X2 = x2;
Y2 = y2;
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2);
H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
}
return *this;
}
Box& Box::operator ()(RECT B)
{
if ((X1 != B.left) && (Y1 != B.top) && (X2 != B.right) && (Y2 != B.bottom))
{
X1 = B.left;
Y1 = B.top;
X2 = B.right;
Y2 = B.bottom;
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2);
H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
}
return *this;
}
bool operator == (const Box &B, const Box &B2) //Are the Boxes the same?
{
return ((B.X1 == B2.X1) && (B.X2 == B2.X2) && (B.Y1 == B2.Y1) && (B.Y2 == B2.Y2));
}
inline bool operator != (const Box &B, const Box &B2) //Are the Boxes Not the same?
{
return !(B == B2);
}
Box Box::operator += (Box B)
{
X1 += B.X1;
Y1 += B.Y1;
X2 += B.X2;
Y2 += B.Y2;
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2);
H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
return *this;
}
Box Box::operator -= (Box B)
{
X1 -= B.X1;
Y1 -= B.Y1;
X2 -= B.X2;
Y2 -= B.Y2;
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2);
H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
return *this;
}
Point Box::MidPointBox(const Box& B)
{
return Point(((B.X1 + B.X2)/2), ((B.Y1 + B.Y2)/2));
}
Box& Box::PointToBox(Point UpperLeft, Point LowerRight)
{
if ((X1 != UpperLeft.X) && (Y1 != UpperLeft.Y) && (X2 != LowerRight.X) && (Y2 != LowerRight.Y))
{
X1 = UpperLeft.X;
Y1 = UpperLeft.Y;
X2 = LowerRight.X;
Y2 = LowerRight.Y;
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2);
H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
}
return *this;
}
Box& Box::operator = (const Box& B)
{
if (this != &B)
{
X1 = B.X1;
Y1 = B.Y1;
X2 = B.X2;
Y2 = B.Y2;
W = (X1 < 0 ? -X1 : X1) + (X2 < 0 ? -X2 : X2);
H = (Y2 < 0 ? -Y2 : Y2) + (Y1 < 0 ? -Y1 : Y1);
}
return *this;
}
bool Box::Contains(Point P) const
{
return (X1 <= P.X && X2 >= P.X && Y1 <= P.X && Y2 >= P.X);
}
bool Box::Contains(const Box& B) const
{
return (X1 <= B.X1 && X2 >= B.X2 && Y1 <= B.Y1 && Y2 >= B.Y2);
}
ostream& operator << (ostream& Str, const Box &B)//For use with Writeln & cout.
{
Str<<"("<<B.X1<<", "<<B.Y1<<", "<<B.X2<<", "<<B.Y2<<")";
return Str;
}
inline Box operator + (Box B1, int S) //Increase Box Size.
{
return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);
}
inline Box operator - (Box B1, int S) //Decrease Box Size.
{
if (((B1.X1 - S) < 0) || ((B1.Y1 - S) < 0) || ((B1.X2 - S) < 0) || ((B1.Y2 - S) < 0))
return Box(-1, -1, -1, -1); //Return -1's if its not a valid box.
else if (((B1.X1 - S) == 0) || ((B1.Y1 - S) == 0) || ((B1.X2 - S) == 0) || ((B1.Y2 - S) == 0))
return Box(0, 0, 0, 0); //Returns 0's because it becomes a point.
else
return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S); //Return the resized box.
}