SFML community forums

Help => System => Topic started by: devhobby on December 23, 2017, 06:55:25 pm

Title: No mathematical operator overloads?
Post by: devhobby on December 23, 2017, 06:55:25 pm
I find the necessity to multiply, divide, add and subtract a vector with a scalar number

Code: [Select]
spriteName.setPosition(Vector2f(myX,myY)/2);

When I try the following code, I get compilation error

Quote
binary '/': no operator found which takes a left-hand operand of type 'sf::Vector2f' (or there is no acceptable conversion)   

So I had to manually make my own (free-function) overload...

I checked on GitHub and there is actually an overload

Line 130 of this github page (https://github.com/SFML/SFML/blob/master/include/SFML/System/Vector2.inl)

Code: [Select]
template <typename T>
inline Vector2<T> operator /(const Vector2<T>& left, T right)
{
    return Vector2<T>(left.x / right, left.y / right);
}

But apparently it doesn't work


Title: Re: No mathematical operator overloads?
Post by: eXpl0it3r on December 23, 2017, 08:44:06 pm
It works, but you need to use matching types.

Vector2f(myX,myY) / 2
       ^            ^
     float         int

Vector2f(myX,myY) / 2.f
       ^             ^
     float         float

And you also need to make sure that the SFML version you use already comes with this change as some overloads weren't part of older SFML 2.x versions.
Title: Re: No mathematical operator overloads?
Post by: devhobby on December 23, 2017, 09:07:54 pm
Thanks, I now have the latest version