C++ 重载函数的调用不明确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4672152/
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
Call of overloaded function is ambiguous
提问by Barshan Das
What does this error message mean?
这个错误信息是什么意思?
error: call of overloaded ‘setval(int)' is ambiguous
huge.cpp:18: note: candidates are: void huge::setval(unsigned int)
huge.cpp:28: note: void huge::setval(const char*)
My code looks like this:
我的代码如下所示:
#include <iostream>
#define BYTES 8
using namespace std ;
class huge {
private:
unsigned char data[BYTES];
public:
void setval(unsigned int);
void setval(const char *);
};
void huge::setval(unsigned int t) {
for(int i = 0; i< BYTES ; i++) {
data[i] = t;
t = t >> 1;
}
}
void huge::setval(const char *s) {
for(int i = 0; i< BYTES ; i++)
data[i] = s[i];
}
int main() {
huge p;
p.setval(0);
return 0;
}
采纳答案by Bart van Ingen Schenau
The literal 0
has two meanings in C++.
On the one hand, it is an integer with the value 0.
On the other hand, it is a null-pointer constant.
字面0
量在 C++ 中有两种含义。
一方面,它是一个值为 0 的整数。
另一方面,它是一个空指针常量。
As your setval
function can accept either an int
or a char*
, the compiler can not decide which overload you meant.
由于您的setval
函数可以接受 anint
或 a char*
,因此编译器无法确定您的意思是哪个重载。
The easiest solution is to just cast the 0
to the right type.
Another option is to ensure the int
overload is preferred, for example by making the other one a template:
最简单的解决方案是将 强制0
转换为正确的类型。
另一种选择是确保int
首选重载,例如通过将另一个作为模板:
class huge
{
private:
unsigned char data[BYTES];
public:
void setval(unsigned int);
template <class T> void setval(const T *); // not implemented
template <> void setval(const char*);
};
回答by Leonardo L.
The solution is very simple if we consider the type of the constant value, which should be "unsigned int" instead of "int".
如果我们考虑常量值的类型,解决方案非常简单,它应该是“unsigned int”而不是“int”。
Instead of:
代替:
setval(0)
Use:
用:
setval(0u)
The suffix "u" tell the compiler this is a unsigned integer. Then, no conversion would be needed, and the call will be unambiguous.
后缀“u”告诉编译器这是一个无符号整数。然后,不需要转换,并且调用将是明确的。
回答by Null Set
replace p.setval(0);
with the following.
替换p.setval(0);
为以下内容。
const unsigned int param = 0;
p.setval(param);
That way it knows for sure which type the constant 0 is.
这样它就可以确定常量 0 是哪种类型。
回答by metamatt
That is ambiguous because a pointer is just an address, so an int can also be treated as a pointer -- 0 (an int) can be converted to unsigned int or char * equally easily.
这是模棱两可的,因为指针只是一个地址,所以 int 也可以被视为指针——0(一个 int)可以同样容易地转换为 unsigned int 或 char *。
The short answer is to call p.setval() with something that's unambiguously one of the types it's implemented for: unsigned int or char *. p.setval(0U), p.setval((unsigned int)0), and p.setval((char *)0) will all compile.
简短的回答是调用 p.setval() ,它明确地是它实现的类型之一:unsigned int 或 char *。p.setval(0U)、p.setval((unsigned int)0) 和 p.setval((char *)0) 都会编译。
It's generally a good idea to stay out of this situation in the first place, though, by not defining overloaded functions with such similar types.
但是,首先避免这种情况通常是个好主意,不要定义具有此类相似类型的重载函数。
回答by Eclipse
Use
用
p.setval(static_cast<const char *>(0));
or
或者
p.setval(static_cast<unsigned int>(0));
As indicated by the error, the type of 0
is int
. This can just as easily be cast to an unsigned int
or a const char *
. By making the cast manually, you are telling the compiler which overload you want.
如错误所示,类型0
为int
。这可以很容易地转换为 anunsigned int
或 a const char *
。通过手动进行强制转换,您可以告诉编译器您想要哪种重载。
回答by Mark Loeser
Cast the value so the compiler knows which function to call:
转换值以便编译器知道要调用哪个函数:
p.setval(static_cast<const char *>( 0 ));
Note, that you have a segmentation fault in your code after you get it to compile (depending on which function you really wanted to call).
请注意,在编译代码后,您的代码中存在分段错误(取决于您真正想要调用的函数)。