Archived

This topic is now archived and is closed to further replies.

Why can't I overload '-' ?

This topic is 6164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites