C++ 编译器错误:对重载函数的调用不明确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6233132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ compiler error: ambiguous call to overloaded function
提问by andandandand
string aux;
int maxy,auxx=0;
cin>>aux;
maxy= (int)sqrt(aux.size());
I'm geting:
我得到:
1> error C2668: 'sqrt' : ambiguous call to overloaded function
1> could be 'long double sqrt(long double)'
1> or 'float sqrt(float)'
1> or 'double sqrt(double)'
Why?
为什么?
回答by littleadv
string::size()
returns size_t
, and sqrt
doesn't accept it in any of its versions. So the compiler has to cast, and cannot choose to what - all of them are OK. You have to put explicit cast:
string::size()
返回size_t
,并且sqrt
不接受它的任何版本。所以编译器必须强制转换,不能选择什么 - 所有这些都可以。你必须把明确的演员:
maxy = (int)sqrt((double)aux.size());
回答by templatetypedef
The problem is that in C++, there are three functions named sqrt
- one taking in a float
, one taking a double
, and one taking a long double
. When you try calling
问题是在 C++ 中,有三个命名的函数sqrt
- 一个接受 a float
,一个接受 a double
,一个接受 a long double
。当你尝试打电话时
sqrt(aux.size());
The compiler tries to determine which of these functions you want to call. Since aux.size()
returns a string::size_type
, which is neither a float
, double
, nor long double
, it tries to see if string::size_type
is implicitly convertible to any of these three. But since string::size_type
is convertible to all three of these types, the compiler marks the call as ambiguous, since it's unclear which of the conversions you want to do.
编译器会尝试确定您要调用这些函数中的哪一个。由于aux.size()
返回 a string::size_type
,它既不是 a float
,double
也不是long double
,它尝试查看是否string::size_type
可以隐式转换为这三个中的任何一个。但是由于string::size_type
可以转换为所有这三种类型,编译器将调用标记为不明确,因为不清楚您想要进行哪种转换。
To fix this, you can explicitly cast aux.size()
to the type that you want. For example:
要解决此问题,您可以显式转换aux.size()
为所需的类型。例如:
sqrt(double(aux.size()));
or
或者
sqrt(float(aux.size()));
This makes the call unambiguously match one of the two functions. Depending on the precision you want, you can choose any of the three overloads. Since you're just casting back to an int
, it's probably fine to cast to a float
here.
这使得调用明确地匹配两个函数之一。根据您想要的精度,您可以选择三种重载中的任何一种。由于您只是投射回int
,因此float
在这里投射到 a 可能没问题。
回答by Gregg
aux.size()
returns an std::size_t
, but sqrt()
does not have an overloaded version that takes a std::size_t
argument.
aux.size()
返回std::size_t
,但sqrt()
没有带std::size_t
参数的重载版本。
The compiler reports that sqrt
has 3 overloads: which take float
, double
and long double
arguments. std::size_t
could be converted to any of those, so there's an ambiguity since the compiler doesn't know whether to convert std::size_t
to float
or double
or long double
.
编译报道称,sqrt
有3个重载:内搭float
,double
和long double
论据。std::size_t
可以转换到任何这些,所以有歧义,因为编译器不知道是否转换std::size_t
到float
或double
或long double
。
回答by david van brink
Try casting your aux.size() to one of those types, so it won't be ambiguous...
尝试将您的 aux.size() 转换为其中一种类型,这样就不会模棱两可了...