Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Function Overloading Similar Conversions

I'm getting an error which says that two overloads have similar conversions. I tried too many things but none helped.

Here is that piece of code

CString GetInput(int numberOfInput, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
CString GetInput(int numberOfInput, string szTerminationPattern, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);

I can't understand how could string be equal to long?

I'm using Visual C++ 6 (yep I know its old, I'm working on legacy code, so I'm pretty much helpless)

EDIT: The line of code that is triggering the error is

l_szOption = GetInput(13, FALSE, 30 * 10);
like image 653
akif Avatar asked Sep 10 '26 04:09

akif


2 Answers

The problem is caused by the fact that you are supplying the timeout argument as a signed integer value, which has to be converted to an unsigned one for the first version of the function (since the timeout parameter is declared as UINT).

I.e. the first version of the function requires a conversion for the third argument, while the second version of the function requires a conversion for the second argument (FALSE, which is just 0, to string). In this case neither function is better than the other and overload resolution fails.

Try explicitly giving the third argument the unsigned type

l_szOption = GetInput(13, FALSE, 30U * 10);

or

l_szOption = GetInput(13, FALSE, (UINT) 30 * 10);

(whichever you prefer) and the code should compile as expected.

In other words, the compiler is absolutely right to complain about your code. Your code is indeed broken. The problem in your code has exacty the same nature as in the following simple example

void foo(int i, unsigned j);
void foo(unsigned i, int j);

int main() {
  foo(0, 0);
}

This code will also fail to compile for precisely the same reason.

like image 58
AnT Avatar answered Sep 12 '26 17:09

AnT


GetInput(13, FALSE, 30 * 10);

My guess is that

FALSE ==> o ==> NULL is getting converted to std::string(NULL) 

hence, it cannot determine which method to instantiate.

T0 prove this check this :

GetInput(13, TRUE, 30 * 10); //it works

like image 27
aJ. Avatar answered Sep 12 '26 17:09

aJ.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!