我有一个过载的函数:
CString TestFunction(CString s, int number, int optPar1 = 123, int optPar2 = 456) //...
CString TestFunction(int index, CString s, int numbers[], int optPar1 = 123, int optPar2 = 456) //...
CString TestFunction(CString s, int numbers[], int optPar1 = 123, int optPar2 = 456) //...当我这么做时:
第一宗案件:
CString s = TestFunction(someString, anArrayOfIntsWithValues);第二宗案件:
for (int i =0; i < max; i++)
{
CString s = TestFunction(i, someString, anArrayOfIntsWithValues);
}它给了我:
关于第一个案件:
error C2664: 'CString Class1::TestFunction(CString, int, int, int)' : cannot convert parameter 2 from 'const int [2]' to 'int'
5 IntelliSense: no instance of overloaded function "Class1::TestFunction" matches the argument list关于第二个案件:
Error 2 error C2664: 'CString Class1::TestFunction(CString, int,int,int)' : cannot convert parameter 1 from 'int' to 'CString'
6 IntelliSense: no instance of overloaded function "Class1::TestFunction" matches the argument list我是C++新手,我不知道这段代码有什么问题,但它是用C#完美编译的(了解重载函数和可选参数)。
注意:这只是一个真实代码的表示-用户定义的类型被用作参数.
编辑:添加了第二个案例,第一个案例已经得到了回答。
发布于 2017-06-15 03:01:10
首先,参数声明int numbers[]实际上与int* numbers相同。然后,
anArrayOfIntsWithValues是一个const int数组,即const int[2];它可以衰减为const int*,但不能隐式转换为int和int*,然后调用失败。
如果希望调用第二次重载,可以将anArrayOfIntsWithValues的类型更改为int [2],或者将参数numbers的类型更改为const int[] (或const int*),以使它们匹配。
https://stackoverflow.com/questions/44557755
复制相似问题