当传递整数文字 0 时,调用采用 unsigned int 或指针的重载函数是不明确的
这个错误信息是什么意思?
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*)
我的代码如下所示:
class huge {
private:
unsigned char data[8];
public:
void setval(unsigned int) { /* .... */ }
void setval(const char *) { /* ... */ }
};
int main() {
huge p;
p.setval(0);
}
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:
class huge {
private:
unsigned char data[8];
public:
void setval(unsigned int) { /* .... */ }
void setval(const char *) { /* ... */ }
};
int main() {
huge p;
p.setval(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
文字
0
在 C++ 中有两个含义。一方面,它是一个值为 0 的整数。
另一方面,它是一个空指针常量。
由于您的
setval
函数可以接受int
或char*
,因此编译器无法决定您指的是哪个重载。最简单的解决方案是将
0
转换为正确的类型。另一种选择是确保首选 int 重载,例如将另一个重载设置为模板:
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.
As your
setval
function can accept either anint
or achar*
, the compiler can not decide which overload you meant.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:如果我们考虑常量值的类型,解决方案非常简单,应该是“unsigned int”而不是“int”。
而不是:
使用:
后缀“u”告诉编译器这是一个无符号整数。那么,就不需要任何转换,并且调用将是明确的。
The solution is very simple if we consider the type of the constant value, which should be "unsigned int" instead of "int".
Instead of:
Use:
The suffix "u" tell the compiler this is a unsigned integer. Then, no conversion would be needed, and the call will be unambiguous.
将
p.setval(0);
替换为以下内容。这样它就可以确定常量 0 是什么类型。
replace
p.setval(0);
with the following.That way it knows for sure which type the constant 0 is.
使用
or
正如错误所示,
0
的类型是int
。这可以很容易地转换为unsigned int
或const char *
。通过手动进行强制转换,您可以告诉编译器您想要哪个重载。Use
or
As indicated by the error, the type of
0
isint
. This can just as easily be cast to anunsigned int
or aconst char *
. By making the cast manually, you are telling the compiler which overload you want.转换该值,以便编译器知道要调用哪个函数:
请注意,在编译代码后,代码中存在分段错误(取决于您真正想要调用的函数)。
Cast the value so the compiler knows which function to call:
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).
这是不明确的,因为指针只是一个地址,因此
int
也可以被视为指针 - 0(int
)可以转换为unsigned int
或char *
同样容易。简短的答案是使用明确是其实现类型之一的内容来调用 p.setval() :
unsigned int
或char *
。p.setval(0U)
、p.setval((unsigned int)0)
和p.setval((char *)0)
都会编译。不过,首先避免这种情况的发生通常是一个好主意,不要定义具有此类相似类型的重载函数。
That is ambiguous because a pointer is just an address, so an
int
can also be treated as a pointer – 0 (anint
) can be converted tounsigned int
orchar *
equally easily.The short answer is to call
p.setval()
with something that's unambiguously one of the types it's implemented for:unsigned int
orchar *
.p.setval(0U)
,p.setval((unsigned int)0)
, andp.setval((char *)0)
will all compile.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.