C++函数重载类似转换

发布于 2024-08-16 01:45:34 字数 534 浏览 8 评论 0原文

我收到一个错误,指出两个重载具有相似的转换。我尝试了太多的事情,但没有任何帮助。

这是我无法理解的那段代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

歌枕肩 2024-08-23 01:45:34

该问题是由于您将超时参数作为有符号整数值提供的,对于函数的第一个版本,必须将其转换为无符号整数值(因为超时参数已声明)作为UINT)。

即函数的第一个版本需要对第三个参数进行转换,而函数的第二个版本需要对第二个参数进行转换(FALSE,即 0 ,到字符串)。在这种情况下,两个函数都不比另一个函数更好,并且重载解析失败。

尝试明确地将第三个参数指定为无符号类型

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

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

(无论您喜欢哪个),代码应该按预期编译。

换句话说,编译器抱怨你的代码是绝对正确的。你的代码确实被破坏了。您的代码中的问题与以下简单示例具有完全相同的性质。

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

int main() {
  foo(0, 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 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.

○愚か者の日 2024-08-23 01:45:34
GetInput(13, FALSE, 30 * 10);

我的猜测是

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

,因此它无法确定实例化哪种方法。

T0 证明这一点,检查一下:

GetInput(13, TRUE, 30 * 10); //它有效

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

萝莉病 2024-08-23 01:45:34

您可能会向该函数传递第二个参数,该参数既不是 BOOL,也不是 string,而是可以隐式转换为任一类型的类型。

例如,字符指针。

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.

独守阴晴ぅ圆缺 2024-08-23 01:45:34

要解决调用函数时的歧义,请将第二个参数强制转换为 BOOL 或使用 string("whatever")(如果确实是 st​​d::string)。

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.

长梦不多时 2024-08-23 01:45:34

考虑以下情况:

BOOL 是 int 的 typedef。

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

'a' 是否要转换为 BOOL 还是字符串???

当您进行函数调用时,通过使用 static_cast 给出正确的参数来调用所需的函数。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文