Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: No mathematical operator overloads?  (Read 3640 times)

0 Members and 1 Guest are viewing this topic.

devhobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
No mathematical operator overloads?
« 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

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



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: No mathematical operator overloads?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

devhobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: No mathematical operator overloads?
« Reply #2 on: December 23, 2017, 09:07:54 pm »
Thanks, I now have the latest version