C++ type-cast


I’m trying to load a custom matrix that I define as follows:
GLfloat matrix[16] = {cos(45), 0, -sin(45), 0, 0, 2, 0, 0,sin(45), 0, cos(45), 0,0, cos(45), 0, 1};

but I keeping getting this compile-time error:
error C2668: ‘cos’ : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(551): could be ‘long double cos(long double)’
1> c:\program files\microsoft visual studio 8\vc\include\math.h(503): or ‘float cos(float)’
1> c:\program files\microsoft visual studio 8\vc\include\math.h(116): or ‘double cos(double)’

Anyone know how I can get around this?



The compiler cannot derive the type of the constants automatically. Use numbers like 45.0f, 2.0f etc. to specify single-precision float consts.

try cos(45.0f)

The constants are well defined in that case. The problem is that there is no overload for those functions that takes integers (which is the type of the constants), and parameter conversions exists for more than one overloaded function.

The solution is correct though, but the reason for it was wrong.

Bob, this is exactly what I was saying… the compiler can’t derive which type was meant, as integer could be converted both to single or double precision. The constants themselves are of course well defined, but the types mismatch (at least formally). It is another story that in most (unambiguous) cases such situations are unnoticeable, as compiler can provide the correct conversion.

That’s something different. The missing “was meant” from your first post makes, in my oppinion, quite a different to the meaning of it. That’s what I was commenting on. That is, it can determine the type of the constants, but it cannot (as you say) determine which type was meant.

Ok, I admit, my first post could be read that way… I need to learn how to express my thoughts more precisely :frowning: