Build error: none of the 12 overloads could convert all argument types

I am using an overload function which is defined in one of my header files

// indexed variables: todo overloads
extern int PmdgGetVariable(char *variableName, int index, bool* result);
extern int PmdgGetVariable(char *variableName, int index, short* result);
extern int PmdgGetVariable(char *variableName, int index, unsigned short* result);
extern int PmdgGetVariable(char *variableName, int index, int* result);
extern int PmdgGetVariable(char *variableName, int index, unsigned int* result);
extern int PmdgGetVariable(char *variableName, int index, float* result);

      

I get an error when trying these arguments

int res = PmdgGetVariable("MCP", 0, 0);

      

mistake

main.cpp(80): error C2665: 'PmdgGetVariable' : none of the 12 overloads could convert all the argument types
1>          c:\users\roar\documents\visual studio 2010\projects\iocpclient_ngx v0.3\PmdgSiocConnector.h(27): could be 'int PmdgGetVariable(char *,int,bool *)'
1>          c:\users\roar\documents\visual studio 2010\projects\iocpclient_ngx v0.3\PmdgSiocConnector.h(28): or       'int PmdgGetVariable(char *,int,short *)'
1>          c:\users\roar\documents\visual studio 2010\projects\iocpclient_ngx v0.3\PmdgSiocConnector.h(29): or       'int PmdgGetVariable(char *,int,unsigned short *)'
1>          c:\users\roar\documents\visual studio 2010\projects\iocpclient_ngx v0.3\PmdgSiocConnector.h(30): or       'int PmdgGetVariable(char *,int,int *)'
1>          c:\users\roar\documents\visual studio 2010\projects\iocpclient_ngx v0.3\PmdgSiocConnector.h(31): or       'int PmdgGetVariable(char *,int,unsigned int *)'
1>          c:\users\roar\documents\visual studio 2010\projects\iocpclient_ngx v0.3\PmdgSiocConnector.h(32): or       'int PmdgGetVariable(char *,int,float *)'
1>          while trying to match the argument list '(const char [4], int, int)'

      

I am new to C ++ and overloads. What am I doing wrong? What should I have as a third argument?

RGS

+3


source to share


5 answers


There are two problems here.

"MCP"

has a type char const[4]

that splits into char const*

. This is incompatible with the first argument char*

. Perhaps you need to fix these function signatures to accept arguments char const*

?

If the functions are not yours and correct (i.e. they really need to change the first argument), you need to pass the argument that changed:

char mcp[] = "MCP"; // now this properly decays to char*
int res = PmdgGetVariable(mcp, 0, 0); // beware of buffer overflow issues

      

If the function signatures are incorrect because they do not change the first argument despite using a non-constant parameter, you can resort to const_cast

.



Another problem is that it is 0

possible to convert to all of these pointer types. The compiler cannot decide which of these functions to choose. You need to either explicitly specify the cast type or use a variable with the appropriate type.

PmdgGetVariable(mcp, 0, static_cast<int*>(0));

      

It seems strange to me that this function can be called using a null pointer and have all these overloads. Maybe this requires a non-null pointer?

int x = 0;
int res = PmdgGetVariable(mcp, 0, &x);

      

+7


source


The problem is with the first parameter. Character literals ( "MCP"

in your example) are of type const char*

and there is no overload that has const char*

as its first argument.



+1


source


I would say this is because your call is ambiguous. Since you are passing nullptr

as the third parameter with no type information, any of the overloads shown may be correct.

To fix, try something like:

bool fParam = true;
int res = PmdgGetVariable("MCP", 0, &fParam );

      

0


source


All of your overloads have a pointer as their third parameter, so if you don't pass in a pointer, the compiler will generate an error.

What you are doing wrong, you do not know. What should this function do? Is it supposed to return the result in the calling var passed by reference? I'm guessing because the third parameter. "result", so:

bool myResult;
..
int res = PmdgGetVariable("MCP", 0, &myResult);

      

.. or any of the other 5 overloaded result types.

0


source


Because when you write:

    PmdgGetVariable("MCP", 0, 0);

      

Just by looking at this line and knowing that there are many possible such functions, how do you know if you mean PmdgGetVariable with the third parameter bool *? short *? INT * ?. When you specify literal '0', this literal can refer to any of these types

0


source







All Articles