1

My intention was to overload a function to handle optional parameters. My code looks like

void functionA(const char *errorMsg) {
 functionA('0', errorMsg);
}
void functionA(char errorCode, const char *errorMsg) {
 ...
}

The line functionA('0',... generates the error

invalid conversion from 'char' to 'const char*'

How do I fix that?

asked Dec 18, 2015 at 5:53

2 Answers 2

1

The compiler needs the prototypes of the functions before the body containing the call to the overloaded function otherwise if starts complaining that the parameters does not match the known function (at this point in the source code).

// The function prototypes
void functionA(const char *errorMsg);
void functionA(char errorCode, const char *errorMsg);
...
void functionA(const char *errorMsg) 
{
 functionA('0', errorMsg);
}
void functionA(char errorCode, const char *errorMsg) 
{
 ...
}

Cheers!

answered Dec 18, 2015 at 7:29
1

What do your prototypes look like? I suspect that the compiler thinks that it is calling the first instance of functionA() and is trying to cast '0' to a 'const char *` and that isn't going to work...

If you don't have prototypes for both signatures, then adding the second one may solve your problem. If you do, I'm puzzled.

answered Dec 18, 2015 at 6:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.