gpt4 book ai didi

c++ - 如果使用 unsigned,为什么重载会导致调用不明确?

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:28 24 4
gpt4 key购买 nike

void foo(int,int) {}
void foo(int ,float) {}
void foo(float,int) {}

void main()
{
unsigned int i = 10;
unsigned float f = 1.0; //line 5
foo(i,f); // ambiguous call error
}

将第 5 行替换为

float f = 1.0;

使程序运行。为什么会这样?

我正在使用 visual studio 2005。

最佳答案

没有unsigned float这样的数据类型。注意编译器警告;如果您没有提高警告级别。从在 Visual Studio 2010 上运行此示例来看,编译器似乎忽略了声明中的 float 关键字

unsigned float f = 1.0;

这使得 f 成为 unsigned int。由于您没有采用 intunsigned intfoo() 重载,因此编译器无法推断要调用哪个重载.如果再添加一个重载

void foo(int,unsigned int) {}

模棱两可的调用错误消失了。

关于c++ - 如果使用 unsigned,为什么重载会导致调用不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6722333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com