C++ “一个错误”;调用'';是模棱两可的",;,虽然该类';s的构造函数参数看起来不一样吗?
出现一条错误消息,C++ “一个错误”;调用'';是模棱两可的",;,虽然该类';s的构造函数参数看起来不一样吗?,c++,class,compiler-errors,C++,Class,Compiler Errors,出现一条错误消息,调用“Binary”的构造函数不明确,该错误消息仅在macOS上使用LLVM编译器时出现,但在windows上不会出现。 此外,类的构造函数参数看起来也不一样 class Binary { public: Binary() = default; Binary(uintmax_t containerSize); Binary(unsigned char binary); Binary(std::initializer_list<unsigne
调用“Binary”的构造函数不明确
,该错误消息仅在macOS上使用LLVM
编译器时出现,但在windows上不会出现。此外,类的构造函数参数看起来也不一样
class Binary {
public:
Binary() = default;
Binary(uintmax_t containerSize);
Binary(unsigned char binary);
Binary(std::initializer_list<unsigned char> binaryList);
// .....
};
// When using
// fileSize is `std::streamoff` data type
Binary fileContent((unsigned long long)fileSize) // << This line is causing the problem.
类二进制{
公众:
Binary()=默认值;
二进制(uintmax\t containerSize);
二进制(无符号字符二进制);
二进制(std::初始值设定项\列表二进制列表);
// .....
};
//使用时
//文件大小为'std::streamoff'数据类型
二进制文件内容((无符号长)文件大小)//uintmax\u t
是计算机上最大宽度无符号整数类型的typedef。编译代码时,如果该类型不完全是unsigned long long
,则此调用:
Binary fileContent((unsigned long long)fileSize);
是不明确的,因为参数需要经过一次转换才能匹配以下任一构造函数:
Binary(uintmax_t containerSize); // conversion from unsigned long long to uintmax_t needed
Binary(unsigned char binary); // conversion from unsigned long long to unsigned char needed
编译器无法在它们之间进行选择,因此出现了一个错误
如果uintmax\u t
恰好是unsigned long long
,则第一个构造函数是完全匹配的,并且被选中,程序编译。这可能是macOS和您看到的Windows编译器版本之间的差异。如果目的是调用第二个ctor,为什么要转换为无符号long long
而不是uintmax\t
?我打赌在你的Windows版本上,这些根类型是相同的,但在你的MacOS版本上不同。uintmax\u t
是unsigned long
,所以我认为unsigned long
是合适的,而且,我在MacOS和Windows上做了同样的事情,但Windows没有显示这条消息。是的,我们明白了;你在帖子里也这么说了。现在再次阅读我所说的内容,特别是不同平台上的uintmax\u t
不一定与unsigned long
同义,因此您的第一句话,uintmax\u t
是unsigned long
并非普遍正确。对不起,但是uintmax\u t
和unsigned char
之间有什么相似之处使编译器认为两者是相同的呢?不,在uintmax\u t
和unsigned char
之间没有相似之处。事实上,它们与unsigned long long
有着相同的区别,这就是编译器无法选择的原因。编辑了答案,希望有帮助。我发现uintmax\t
是这样声明的typedef long unsigned int uintmax\t
和typedef long long unsigned int uintmax\t
。是的,第一个需要转换,第二个是精确匹配。