Code compiled in VS 2002 but not VS 2003 - error C2668

F

Francisco Rivas

Hello all,

I did a search through the archives and found that someone posted a message
about this same error, but never got a response.

I've got a small sample app that compiles and runs in VS 2002 but doesn't
compile in VS 2003. In VS 2003, I get error C2668. The error comes from
trying to use the sqrt function.

I noticed this error when I tried to recompile a larger application
developed in VS 2002. I created this small sample to reproduce the error.

Can someone tell me why I get this error and how to fix it?

Thanks,
Francisco

// Sample app
#include <iostream>
#include <cmath>

void main()
{
unsigned int numSamples = 25;
int sqrtSamples = sqrt( numSamples );
std::cout<<"sqrt="<<sqrtSamples<<std::endl;
}
 
D

David Lowndes

I've got a small sample app that compiles and runs in VS 2002 but doesn't
compile in VS 2003. In VS 2003, I get error C2668. The error comes from
trying to use the sqrt function.
Can someone tell me why I get this error and how to fix it?

Francisco,

You could add a cast to remove the ambiguity:

int sqrtSamples = (int) sqrt( (float) numSamples );

Dave
 
R

Rudy Ray Moore

The compiler doesn't know if you want to take the sqrroot of a 32 bit float
or a 64 bit float (double).

The solution is to not take the square root of an int. Instead, take the
square root of a float or a double using a cast or by changing your variable
numSamples to a float or a double.
 
F

Francisco Rivas

Thank you for the response. I guess I'll just cast it to a float then.

Francisco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top