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?

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!

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.