最简单的解决方案是将 0 强制转换为正确的类型.另一种选择是确保首选 int 重载,例如通过将另一个作为模板:类巨大{私人的:无符号字符数据[字节];上市:void setval(unsigned int);模板void setval(const T *);//未实现模板 <>void setval(const char*);};What does this error message mean?error: call of overloaded ‘setval(int)’ is ambiguoushuge.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 8using 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;} 解决方案 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 an int or a char*, 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: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*);}; 这篇关于重载函数的调用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-18 08:27