1

Ok, so I'm aware of the typical reason people get the prototyped function not called error. This is not that case

Consider the following class:

class FF {
public:
   FF(float f):_f(f){}
   float _f;

   void DoSomething(float f) { _f = f; }
};

I attempt to construct an FF object using the following code which casts an int to a float. Note: I am working with legacy code here which uses this casting style - I know it is not the modern way.

int x = 0;
FF fTest(float(x));

VS2013 gives the warning :'FF fTest(float)': prototyped function not called (was a variable definition intended?)

Why does VS think this is a function prototype? FF only contains 1 constructor.

Interestingly, the same code format gives no warnings when used for calling functions on the object:

int x = 0;
FF fTest(0.f);
fTest.DoSomething(float(x));
  • In C++, if a statement is valid as a declaration, then it interpreted as such (for backward compatibility with C). Using static_cast or defining a temporary variable for the floating point value would solve the problem. – Phil1970 Jun 01 '17 at 23:02
  • By the way `FF fTest(float (x));` is equivalent to `FF fTest(float x);` This is a prototype of a function that take a `float` and return an object of type `FF`. By the way, **if you would really have used old cast**, it would have been `FF fTest((float) x);` and this cannot be a valid declaration a x if a variable (and not a parameter name) so it must be a constructor call. Adding an extra pair of parenthesis as in `FF fTest((float(x)));` also solve the problem. `FF fTest{ float(x) };` (uniform initialisation, modern C++) and `FF fTest(x);` (with a warning) also call the constructor. – Phil1970 Jun 01 '17 at 23:42

0 Answers0