'当多个重载函数实例"sqrt"匹配参数列表'时,我该怎么办?

use*_*810 4 c++ overloading sqrt

我在for循环中的代码中出错,for ( j = 3; j <=sqrt(num); j +=2):

多个重载函数"sqrt"的实例与参数列表匹配.

我该如何解决?

# 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;
}
Run Code Online (Sandbox Code Playgroud)

mfo*_*ini 10

尝试:

for (j = 3; j <= std::sqrt(static_cast<float>(num)); j +=2)
Run Code Online (Sandbox Code Playgroud)

发生的事情是<cmath>包含3个不同的sqrt定义,编译器不知道您尝试使用哪个.