C++函数重载类似转换
我收到一个错误,指出两个重载具有相似的转换。我尝试了太多的事情,但没有任何帮助。
这是我无法理解的那段代码,
CString GetInput(int numberOfInput, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
CString GetInput(int numberOfInput, string szTerminationPattern, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
string 为何等于long?
我正在使用 Visual C++ 6(是的,我知道它很旧,我正在处理遗留代码,所以我非常无助)
编辑:触发错误的代码行是
l_szOption = GetInput(13, FALSE, 30 * 10);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该问题是由于您将超时参数作为有符号整数值提供的,对于函数的第一个版本,必须将其转换为无符号整数值(因为超时参数已声明)作为
UINT
)。即函数的第一个版本需要对第三个参数进行转换,而函数的第二个版本需要对第二个参数进行转换(
FALSE
,即0
,到字符串
)。在这种情况下,两个函数都不比另一个函数更好,并且重载解析失败。尝试明确地将第三个参数指定为无符号类型
或
(无论您喜欢哪个),代码应该按预期编译。
换句话说,编译器抱怨你的代码是绝对正确的。你的代码确实被破坏了。您的代码中的问题与以下简单示例具有完全相同的性质。
由于完全相同的原因,该代码也将无法编译。
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 just0
, tostring
). In this case neither function is better than the other and overload resolution fails.Try explicitly giving the third argument the unsigned type
or
(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
This code will also fail to compile for precisely the same reason.
我的猜测是
,因此它无法确定实例化哪种方法。
T0 证明这一点,检查一下:
GetInput(13, TRUE, 30 * 10);
//它有效My guess is that
hence, it cannot determine which method to instantiate.
T0 prove this check this :
GetInput(13, TRUE, 30 * 10);
//it works您可能会向该函数传递第二个参数,该参数既不是
BOOL
,也不是string
,而是可以隐式转换为任一类型的类型。例如,字符指针。
You are possibly passing that function a second parameter that is neither a
BOOL
, nor astring
, but a type that could be implicitly converted to either.A character pointer, for example.
要解决调用函数时的歧义,请将第二个参数强制转换为
BOOL
或使用string("whatever")
(如果确实是 std::string)。To resolve the ambiguity when you call the function either cast the second parameter to
BOOL
or usestring("whatever")
if that is indeed std::string.考虑以下情况:
BOOL 是 int 的 typedef。
'a' 是否要转换为 BOOL 还是字符串???
当您进行函数调用时,通过使用 static_cast 给出正确的参数来调用所需的函数。
Consider following case :
BOOL is typedef of int.
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.