可能重复:
C++ compiler error: ambiguous call to overloaded function
刚刚将一些代码从pdf复制到C ++ builder XE2和visual studio express 2012.两个编译器都给出了关于ambiquity的错误代码。我刚刚开始,所以我真的不知道该怎么做。也许我的教科书(pdf)现在已经过时了?它被称为“14天内学习c ++”。好吧无论如何这里是复制的代码。
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#pragma hdrstop
void getSqrRoot(char* buff, int x);
int main(int argc, char** argv)
{
int x;
char buff[30];
cout << “Enter a number: “;
cin >> x;
getSqrRoot(buff, x);
cout << buff;
getch();
}
void getSqrRoot(char* buff, int x)
{
sprintf(buff, “The sqaure root is: %f”, sqrt(x));
}
我在c ++ builder中得到的错误代码是:
[BCC32错误] SquareRoot.cpp(19):E2015'std :: sqrt(float)在c:\ program files(x86)\ embarcadero \ rad studio \ 9.0 \ include \ windows \ crtl \ math之间的歧义。 h:266'和'std :: sqrt(long double)在c:\ program files(x86)\ embarcadero \ rad studio \ 9.0 \ include \ windows \ crtl \ math.h:302' 完整的解析器上下文 SquareRoot.cpp(18):解析:void getSqrRoot(char *,int)
在旁注中,我的pdf手册中的引号与普通的“我输入的字符不同。这些”也与编译器不兼容。也许有人也知道这个问题吗?提前感谢。
答案 0 :(得分:2)
更改您的代码:
void getSqrRoot(char* buff, int x)
{
sprintf(buff, “The sqaure root is: %f”, sqrt((float)x));
}
因为平方根被重载,函数编译器没有机会从int x值隐式转换为float或double值,你需要直接进行。
Compiler: see sqrt(int) -> what to choose? sqrt(float)/sqrt(double) ?
Compiler: see sqrt((float)int) -> sqrt(float), ok!
Compeler: see sqrt((double)int) -> sqrt(double), ok!
答案 1 :(得分:0)
将getSqrRoot函数更改为以下
void getSqrRoot(char* buff, float x)
{
同样在第一行修复声明。
这种情况正在发生,因为std::sqrt
是您用来获取平方根的函数,可以使用float
或double
,但您已经为它{{1}因为编译器现在不知道要调用哪个函数,所以会导致混淆。