having problam using sin and cos functions

Started by
8 comments, last by Esh 17 years ago
hi all, there is a part in my code where i'm using sin and cos functions when i'm compiling i get the next compile error: error C2668: 'sin' : ambiguous call to overloaded function error C2668: 'cos' : ambiguous call to overloaded function anyway here is the code:

int iPower = (int)(sin(iY * iX) * 128 + cos(iY * -iX) * 128);
			piSurfaceBuffer[iX + iY * (kLockedRect.Pitch >> 2)] = 
								D3DCOLOR_XRGB(iPower, iPower, iPower);
do you got any idea of what the problam is? thanks.
Advertisement
What is the type of iX * iY?
Varaibles iX and iY are of type int, so is iX*iY, but sin and cos is overloaded for float and double. The compiler cannot decide which version of sin/cos should be called, since the int can be implicitly converted to a float and to a double.
You should probably also note that sin and cos assume that their argument is in radians, not in degrees.
OK. i changed the iX and iY type to float.
but now i get the next compile error:

error C2108: subscript is not of integral type

this is the related code:
D3DLOCKED_RECT kLockedRect;<...>piSurfaceBuffer[iX + iY * (kLockedRect.Pitch >> 2)] = 		D3DCOLOR_XRGB(iPower, iPower, iPower);


and iPower type is int.





piSurfaceBuffer is an array! and you access a float index inside it.
It's the same as if you wrote: piSurfaceBuffer [5.3f] = 10;

It's also funny that the variables iX and iY are floats even though they start with 'i' in the beginning. Don't change the type of your variables just because of a compiler error, that is the worst solution because it ruins the logic of your algorithm (no matter what you are trying to do there). The type of the variables should match the purpose of your program and NOT the requests of the compiler. The solution I recommend is to keep iX and iY as integers and the first compiler error should be solved by simply casting the iX and iY to floats only inside the sin/cos.
Quote:Original post by UriKiller
...


This post marks the first time I've rated someone <1000 rating up. Congratulations!
Thanks :)

By The way:
I got my low rating when I've been banned for some time after posting the same post in about all the existing forums in Gamedev. I had no idea that it was forbidden...
also, don't use hungarian notation in variable names. Only stubborn old-schoolers do this. Embedding the type in the variable name means rigid, inflexible code. Instead choose a good name that represents what the data is used for and not what type it happens to currently be.
Quote:Original post by UriKiller
piSurfaceBuffer is an array! and you access a float index inside it.
It's the same as if you wrote: piSurfaceBuffer [5.3f] = 10;

It's also funny that the variables iX and iY are floats even though they start with 'i' in the beginning. Don't change the type of your variables just because of a compiler error, that is the worst solution because it ruins the logic of your algorithm (no matter what you are trying to do there). The type of the variables should match the purpose of your program and NOT the requests of the compiler. The solution I recommend is to keep iX and iY as integers and the first compiler error should be solved by simply casting the iX and iY to floats only inside the sin/cos.


yeah.. that's what i did.
thanks.

This topic is closed to new replies.

Advertisement