Reputation: 41
I use an overload function that is defined as follows 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 from the build prosess when trying these arguments
int res = PmdgGetVariable("MCP", 0, 0);
error is
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/can I have as the 3rd argument?
rgs
Upvotes: 3
Views: 26503
Reputation: 772
Because when you write:
PmdgGetVariable("MCP", 0, 0);
Just looking at this line and knowing there are many possible such functions, how do you know if you're referring to the PmdgGetVariable with the third parameter bool*? short*? int*?. When you specify the literal '0', that literal could be referring to any of those types
Upvotes: 0
Reputation: 234654
There are two issues here.
"MCP"
has type char const[4]
which decays into char const*
. This is not compatible with the first argument char*
. Maybe you need to fix those function signatures to take char const*
arguments?
If the functions are not yours and are correct (i.e., they actually need to change the first argument), then you need to pass an argument that is mutable:
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 not correct because they don't change the first argument, despite taking a non-const parameter, you can resort to const_cast
.
The other problem is that 0
can be converted to all those pointer types. There is no way for the compiler to decide which one of those functions to pick. You need to either be explicit about the type with a cast, or use a variable with the proper type.
PmdgGetVariable(mcp, 0, static_cast<int*>(0));
I find it strange that the function can be called with a null pointer and has all those overloads. Maybe it requires a non-null pointer?
int x = 0;
int res = PmdgGetVariable(mcp, 0, &x);
Upvotes: 8
Reputation: 24897
All your overloads have a pointer as the third parameter & so, if you don't pass a pointer, the compiler will generate an error.
What are you doing wrong - don't know. What is this function supposed to do? Is it supposed to return a result into a caller var passed by reference? I'm guessing so because the third param. is names 'result', so:
bool myResult;
..
int res = PmdgGetVariable("MCP", 0, &myResult);
..or any of the other 5 overloaded result types.
Upvotes: 0
Reputation: 6358
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.
Upvotes: 1
Reputation: 41017
I'd say it is because your call is ambiguous. Since you are passing a nullptr
as your third parameter without type information, any of the overloads shown could potentially be correct.
To fix, try something like:
bool fParam = true;
int res = PmdgGetVariable("MCP", 0, &fParam );
Upvotes: 0