Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I do when 'more than one instance of overloaded function "sqrt" matches the argument list'?

I get an error in my code in the for loop, for ( j = 3; j <=sqrt(num); j +=2):

more than one instance of overloaded function "sqrt" matches the argument list.

How do I resolve that?

# include <cmath>

// determine if number is prime
bool isPrime (long n) 
{
  int j, num = 0;
  {
    if (num <=1)
      return false;
  }
  for ( j = 3; j <=sqrt(num); j +=2)
  {
    if (num % j == 0)
      return false;
  }   
  return true;
}
like image 246
user1231810 Avatar asked Jan 18 '23 09:01

user1231810


1 Answers

Try:

for (j = 3; j <= std::sqrt(static_cast<float>(num)); j +=2)

What is happening is that <cmath> contains 3 different definitions of sqrt and the compiler doesn't know which one you are trying to use.

like image 55
mfontanini Avatar answered Mar 04 '23 12:03

mfontanini