Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “一个错误”;调用'';是模棱两可的",;,虽然该类';s的构造函数参数看起来不一样吗?_C++_Class_Compiler Errors - Fatal编程技术网

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
。是的,第一个需要转换,第二个是精确匹配。