Search code examples
c++overloadingoverload-resolution

Ambiguous call to overloaded sqrt function when passing size_t


string aux;
int maxy, auxx = 0;

cin >> aux;

maxy = (int)sqrt(aux.size());

Why am I getting this error:

1> error C2668: 'sqrt' : ambiguous call to overloaded function
1>        could be 'long double sqrt(long double)'
1>        or       'float sqrt(float)'
1>        or       'double sqrt(double)'

Solution

  • string::size() returns size_t, and sqrt doesn't accept it in any of its versions. So the compiler has to cast, and cannot choose to what - all of them are OK. You have to put explicit cast:

    maxy = (int)sqrt((double)aux.size());