Skip to main content
gamedev.net
🔒 Locked

operator overloading help needed

Started by MARS_999 4 replies 1.1k views
Original Post
MARS_999
MARS_999

template<typename T> inline vec2<T> operator *(const vec2<T>& lhs, const vec2<T>& rhs)
{
    return vec2<T>(lhs.x * rhs.x, lhs.y * rhs.y);
}

result = pos * posa;


I get this error error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) I am not sure what else to do, but I am using template classes and trying to multiple two class vectors with a global operator and having no luck... Thanks
MARS_999
MARS_999
Unless someone has a fix for the binary operator overloading....

I am using this for now.

static inline vec2 Multiply(const vec2& lhs, const vec2& rhs)	{		return vec2(lhs.x * rhs.x, lhs.y * rhs.y);	}

visitor
visitor
The error message seems to indicate that the result of pos * posa is an int. That could happen if pos and posa are indeed int's...

Could you post a more complete example?
phresnel
phresnel
You could also utilise friend name injection, which makes the type-id easier:

template<typename T> class vec2 {public:    friend inline vec2 operator *(const vec2& lhs, const vec2& rhs) {        return vec2(lhs.x * rhs.x, lhs.y * rhs.y);    } };


Thanks to ADL this works as if you had defined it globally. Also, using this doesn't make vec2 a POD, if that is intended.
MARS_999
MARS_999
I got it working, for some reason I missed overloading the = operator... All is great in operator overloading land... :)

Thanks all.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.