views:

232

answers:

5

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);
+1  A: 

You are possibly passing that function a second parameter that is neither a BOOL, nor a string, but a type that could be implicitly converted to either.

A character pointer, for example.

Shmoopty
+1  A: 

To resolve the ambiguity when you call the function either cast the second parameter to BOOL or use string("whatever") if that is indeed std::string.

Igor Zevaka
Casting `FALSE` to `BOOL` will not resolve ambiguity in this case. `(BOOL) FALSE` is still a valid form of null-pointer constant, which means that it remains convertible to `std::string`.
AndreyT
+1  A: 

Consider following case :

BOOL is typedef of int.

GetString(10,'a'); // compiler get confused in resolving the function

whether 'a' is to be converted to BOOL or string ???

When you make function call give proper argument by using static_cast to make desired function to call.

char ch = 'a';
GetString(10,static_cast<BOOL>(ch)); // calls function with 2nd argument as BOOL

GetString(10,static_cast<string>(ch)); //calls function with 2nd argument as string
Ashish
+2  A: 
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

aJ
Just the mere fact that `FALSE` is convertible to `std::string` will not make this code fail. The first version of the function has second argument of `BOOL` type, which is better match to `FALSE` than `std::string`. The problem, as I said in my answer, is different. The problem is caused by the third argument: `30 * 10`. It has to have *unsigned* type to make the first function win the overload resolution.
AndreyT
+3  A: 

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.

AndreyT