1

I have this overloaded functions:

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) //...

When I do this:

1st case:

CString s = TestFunction(someString, anArrayOfIntsWithValues);

2nd case:

for (int i =0; i < max; i++)
{
    CString s = TestFunction(i, someString, anArrayOfIntsWithValues);
}

It gives me:

On the 1st case:

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

On the 2nd case:

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

I am new to C++ and I don't know what is wrong with this code, but it was perfectly compiled in C# (with the knowledge of overloaded functions and optional parameters).

Note: this is just a representation of the real code - user-defined types are used as parameters.

EDIT: added a second case and 1st case was already answered.

1
  • Note that using [] in parameter list is archaic and actually the array is not passed by value . So this may differ from your expectations if you are coming from other languages Commented Jun 15, 2017 at 3:44

1 Answer 1

2

At first, the parameter declaration int numbers[] is same as int* numbers in fact. Then,

anArrayOfIntsWithValues is an array of const int, i.e. const int[2]; which could decay to const int*, but can't be implicitly converted to both int and int*, then calling failed.

If you want the 2nd overload to be invoked, you can change the type of anArrayOfIntsWithValues to int [2], or change the type of parameter numbers to const int[] (or const int*), to make them match.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.