Why can't I overload '-' ?
Heya...
I'm using VC++6, & defining an xyz vector class (vector3f). I've hit a slight problem trying to overload - , in both these forms:
friend vector3f operator - (const vector3f& v);
friend vector3f operator - (const vector3f& v1,const vector3f& v2);
The compiler throws up a fatal error C1001 whenever these lines are included in the class header....any suggestions?
Cheers
Catfish
Edited by - Catfish on January 28, 2001 7:22:41 AM
Removing the spaces doesn''t help...
Incidentally, before those 2 problem lines, I have
friend vector3f operator + (const vector3f& v);
which is identical with the exception of the +/- bit, & the compiler seems perfectly happy with it.
Ahhh well - cheers anyway
Catfish
Incidentally, before those 2 problem lines, I have
friend vector3f operator + (const vector3f& v);
which is identical with the exception of the +/- bit, & the compiler seems perfectly happy with it.
Ahhh well - cheers anyway
Catfish
quote: Original post by Catfish
friend vector3f operator - (const vector3f& v);
Are you sure this is supposed to work? If you have a friend function, you''ll need 2 arguments, not one because you don''t have a this pointer. Getting rid of the friend should be done.
quote:
friend vector3f operator - (const vector3f& v1,const vector3f& v2);
This is how an operator should be overloaded via a friend, but I don''t see the point of overloading the ''-'' operator twice, each accepting the same kind of arguments. I think you should just keep the 2nd function, get rid of the first one, as it isn''t necessary. Blame any mistakes I may have made on my newbieness to C++
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
The first overload defined above is to force a negation of the class. It should work as he has it. The second version is to subtract one from the other. It too, should work as is.
Perhaps what is happening is that you are missing a ; after the lines above it?
Regards,
Jumpster
Perhaps what is happening is that you are missing a ; after the lines above it?
Regards,
Jumpster
Regards,JumpsterSemper Fi
Maybe I''m missing something, but if vector3f is a class or struct, then why don''t you just so something like
class vector3f {
public:
// normal vector stuff here
// operators:
vector3f operator - () const; // unary negation
vector3f operator - ( const vector3f& v ) const; // vector sub
vector3f operator + ( const vector3f& v ) const; // vector add
// etc.
};
Friend functions are typically used for things like this:
friend vector3f operator * ( float fScalar,const vector3f& v );
so that you can do scalar * vector. If you had included that operator in your class declaration:
class vector3f {
// blah
vector3f operator * ( float fScalar );
// blah
};
then you could only do vector * scalar.
Hope that helps,
Liquid
class vector3f {
public:
// normal vector stuff here
// operators:
vector3f operator - () const; // unary negation
vector3f operator - ( const vector3f& v ) const; // vector sub
vector3f operator + ( const vector3f& v ) const; // vector add
// etc.
};
Friend functions are typically used for things like this:
friend vector3f operator * ( float fScalar,const vector3f& v );
so that you can do scalar * vector. If you had included that operator in your class declaration:
class vector3f {
// blah
vector3f operator * ( float fScalar );
// blah
};
then you could only do vector * scalar.
Hope that helps,
Liquid
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement