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

Author Topic: Multiplication Operator in Handling time Tutorial  (Read 9929 times)

0 Members and 1 Guest are viewing this topic.

dontpanic

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Multiplication Operator in Handling time Tutorial
« on: August 16, 2020, 02:44:56 pm »
Hello all,

I'm new to C++ and SFML, so going through the tutorials. While playing with the concepts introduced in the "Handling time" tutorial, I got a build error when writing similar code as in the tutorial.

The tutorial presents the code:
sf::Time t1 = ...;
sf::Time t2 = t1 * 2;
Based on that I wrote the code:
sf::Time t = sf::milliseconds(1000);
sf::Time t2 = t * 2;

My code gave me an error: more than one operator "*" matches these operands.

Creating an Int64 object makes explicit the operator definition to use:
sf::Time t = sf::milliseconds(1000);
sf::Int64 multiplier = 2;
sf::Time t2 = t * multiplier;

Just wanted to understand if it's me doing something wrong (the most likely scenario) or if the tutorial is meant to be more pseudocode and I am taking it too literally since the build error is expected in that code.

Thank you in advance and sorry in advance if this thread is misplaced in this forum.

Xiaohu

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiplication Operator in Handling time Tutorial
« Reply #1 on: August 21, 2020, 07:30:02 pm »
Looking at the documentation, the operator* for sf::Time has two overload: one taking float and the other taking Int64. While your argument 2 is convertible to Int64 without any problem (either it's 32 or 64 bits), it's also convertible to float which result in a ambiguity to which method call (even if it's the same expected behaviour, there is two definitions).

You could either try to replace 2 by 2.f to explicitly use the float version or by 2L to use explicitly the long version (which are probably the same). You need probably the 2L version if you're expecting only integers value.
« Last Edit: August 21, 2020, 07:34:04 pm by Xiaohu »

 

anything