获取:错误 C2668:'sqrt':对重载函数的不明确调用
Getting: error C2668: 'sqrt' : ambiguous call to overloaded function
尝试根据教科书中给出的示例构建下面的源代码。我使用的是visualstudio2008。
编译器似乎不知道如何处理sieve:
1>------ Rebuild All started: Project: fig21.40, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'fig21.40', configuration 'Debug|Win32'
1>Compiling...
1>fig21_40.cpp
1>c:usersocukdocumentsc++chapter 21fig21.40fig21.40fig21_40.cpp(27) : error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:program files (x86)microsoft visual studio 9.0vcincludemath.h(581): could be 'long double sqrt(long double)'
1> c:program files (x86)microsoft visual studio 9.0vcincludemath.h(533): or 'float sqrt(float)'
1> c:program files (x86)microsoft visual studio 9.0vcincludemath.h(128): or 'double sqrt(double)'
1> while trying to match the argument list '(const int)'
1>Build log was saved at "file://c:UsersocukDocumentsC++Chapter 21fig21.40fig21.40DebugBuildLog.htm"
1>fig21.40 - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
代码:
//Fig. 21.40: fig21_40.cpp
//Using a bitset to demonstrate the Sieve of Eratosthenses
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <bitset> //bitset class definition
#include <cmath> //sqrt prototype
int main()
{
const int size=1024;
int value;
std::bitset<size> sieve;
sieve.flip();
//perform Sieve of Eratosthenses
int finalBit = sqrt( sieve.size() ) + 1;
for(int i=2; i<finalBit; ++i)
if(sieve.test(i))
for(int j=2*i; j<size; j+=i)
sieve.reset(j);
cout << "The prime numbers in the range 2 to 1023 are:n";
//display prime numbers in range 2-1023
for(int k=2, counter=0; k<size; ++k)
if(sieve.test(k)){
cout <<setw(5)<<k;
if(++counter % 12 ==0)
cout << 'n';
}//end outer if
cout << endl;
//get value from user to determine whether value is prime
cout << "nEnter a value from 1 to 1023(-1 to end): ";
cin>>value;
while(value != -1){
if(sieve[value])
cout << value << " is a prime numbern";
else
cout << value << " is not a prime numbern";
cout << "nEnter a value from 2 to 1023 (-1 to end): ";
cin >> value;
}//end while
return 0;
}
我认为这是因为在C++
的较新版本中,sqrt
重载(参数可以是double
、float
或long double
),并且您传递了一个int。只需将参数强制转换为double
即可明确:
int finalBit = sqrt( (double) (sieve.size()) ) + 1;
sqrt
函数有以下重载:
float sqrt(float arg);
double sqrt(double arg);
long double sqrt(long double arg);
由于sieve.size()
返回size_t
,编译器无法选择进行哪种转换—"float"、double
或long double
。
添加强制转换以解决此问题。考虑到你的尺码范围,你选择什么类型并不重要。
int finalBit = sqrt( (double)sieve.size() ) + 1;
// ^^^^^^^^
// float or long double would work as well.
相关文章:
- 为函数定义符号不明确的指针参数
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 对重载函数find_first_not_of的不明确调用
- 调用'Node'构造函数是不明确的
- "fpclassify":对重载函数的不明确调用
- 删除全局隐式函数 - 避免使用不明确的运算符
- 不明确的函数模板
- 错误:使用复制和交换习惯用法的交换函数中"operator="的重载不明确
- 为什么这种重载函数是不明确的
- 对函数的重新定义和不明确的调用具有数组参数
- C++ 函数名称不明确
- 为什么调用具有通用或 r 值引用的重载覆盖函数是不明确的?
- C++ 函数重载不明确,没有自动类型转换
- 不明确的函数声明
- 使用类型标签时C++函数不明确
- 构造函数不明确
- C++:构造函数不明确
- 在模板化函数不明确的情况下表达首选项
- 为什么重载的继承静态函数不明确
- 初始值设定项列表构造函数导致右值构造函数不明确