Sign in to follow this  
Esh

having problam using sin and cos functions

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.





Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this