inline operator overloading

Hi all,
this is my Vector class:
1
2
3
4
5
6
7
8
9
10
class Vector {
    public:
        int X;
        int Y;

        Vector( int x = 0, int y = 0 );

        inline void operator = ( Vector& vec );
        inline void operator += ( Vector& vec );
};


And this is the implementation of the += operator:
1
2
3
4
inline void Vector::operator += ( Vector& vec ) {
	X += vec.X;
	Y += vec.Y;
}


In the main() function I use them like this:
1
2
3
Vector vec;
Vector vec2( 1, 1 );
vec += vec2;


And Code::Blocks gives an error on line 3:
main.cpp|7|undefined reference to `Vector::operator+=(Vector&)'|


Can somebody explain why this is apparently wrong?

PS: when I remove the inline keywords, everything works just fine.
Last edited on
the = and += operators should return a reference to the left hand side object after it's done. This is so you can do silly things like a = b = 1; and have it work just fine, as expected. (Both a and b will be equal to 1) Also, methods in classes should not be declared as inline.

E.g.

1
2
Vector& operator = (const Vector& vec);
Vector& operator += (const Vector & vec);


then implement it as

1
2
3
4
5
6
Vector& Vector::operator+= (const Vector & vec)
{
	this->X += vec.X;  // this is a pointer to the current object.
	this->Y += vec.Y;  // Optional but slightly more clear.
	return *this;
}


e: I made an error, I apologize; It is fixed now.
Last edited on
It seems that the linker does not see the object module where the operator is defined provided that the definition is in a separate module.
Topic archived. No new replies allowed.