gpt4 book ai didi

c++ - 'more than one instance of overloaded function "sqrt“匹配参数列表”怎么办?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:20:23 26 4
gpt4 key购买 nike

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

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

我该如何解决?

# 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;
}

最佳答案

尝试:

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

发生的事情是<cmath>包含 3 个不同的 definitions of sqrt并且编译器不知道您要使用哪个。

关于c++ - 'more than one instance of overloaded function "sqrt“匹配参数列表”怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com