PDA

View Full Version : C++ type-cast



Canadian0469
05-17-2008, 05:35 PM
Hi,

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?

Thanks,

Canadian0469

Zengar
05-17-2008, 05:44 PM
The compiler cannot derive the type of the constants automatically. Use numbers like 45.0f, 2.0f etc. to specify single-precision float consts.

dletozeun
05-17-2008, 05:45 PM
try cos(45.0f)

Bob
05-17-2008, 06:26 PM
The compiler cannot derive the type of the constants automatically. Use numbers like 45.0f, 2.0f etc. to specify single-precision float consts.
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.

Zengar
05-17-2008, 06:35 PM
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.

Bob
05-17-2008, 08:11 PM
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.

Zengar
05-18-2008, 04:55 AM
Ok, I admit, my first post could be read that way... I need to learn how to express my thoughts more precisely :(