• Advertisement
Sign in to follow this  

Operator Overloading

This topic is 4646 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

I'm building a fixed-point class as an exercise, and I've hit a small problem when overloading the equals operator. I need to overload it so that I can convert from the fixed point representation to a floating point representation. Theoretically I think that the function should be declared thus,

namespace FixedPointMath {

class Fixed
{
   ...
   friend float& operator=(float& leftHand, const Fixed& rightHand);
   ...
};

float& operator=(float& leftHand, const Fixed& rightHand)
{
   ...
}

}

But Visual C++ complains when compiling:
error C2801: 'operator =' must be a <Unknown> member
MSDN states that the = operator must be a member of the left-hand class, but how is this possible when using a predefined type? Thanks, Adam

Share this post


Link to post
Share on other sites
Advertisement
You can't provide an assignment operator like that. However you could provide an implicit conversion operator, but be wary of such constructs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement