Color(int, int, int) 与 Color(float, float, float) 不明确调用
如何解决 C++ 中这两者之间的不明确调用?
Color(int, int, int)
Color(float, float, float)
当值被硬编码(即 Color(1, 2, 3)
)和变量 Color(r, g, b)
时,它都是不明确的。为什么编译器不根据数据类型进行解析?以变量形式?
编辑: 抱歉,太多的 C++ 让我忘记了还有其他语言。 并且没有太多关于它的“完整代码”。
float x, y, z;
int r, g, b;
Color(1, 2, 3); // ambiguous
Color(1.0, 2.0, 3.0); // ambiguous
Color(r, g, b); // ambiguous <--- this one is a real pain
Color((int)r, (int)g, (int)b); // ambiguous
Color(x, y, z); //OK
Color(1u, 2u, 3u); //OK
Color(1.0f, 2.0f, 3.0f); //OK
How can I resolve the ambiguous call between these two in C++?
Color(int, int, int)
Color(float, float, float)
It is both ambiguous when the values are hardcoded i.e. Color(1, 2, 3)
and when they are variables Color(r, g, b)
. Why wouldn't the compiler resolve according to data type? In variable form?
EDIT:
Sorry, too much C++ makes me forget there's a other languages.
And there is not much "full code" that was all about it.
float x, y, z;
int r, g, b;
Color(1, 2, 3); // ambiguous
Color(1.0, 2.0, 3.0); // ambiguous
Color(r, g, b); // ambiguous <--- this one is a real pain
Color((int)r, (int)g, (int)b); // ambiguous
Color(x, y, z); //OK
Color(1u, 2u, 3u); //OK
Color(1.0f, 2.0f, 3.0f); //OK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题似乎是您已经声明了
即,所有三个参数都必须是
float
或unsigned
。如果您尝试使用其他类型(例如int
或double
)来调用它,它是不明确的——编译器不知道您想要哪个,因为两者都只是一个好的类型。 (或者如果你愿意的话也可以很糟糕)。您可以通过声明更多重载来改进一些事情:但是如果尝试使用混合类型调用它,您仍然会遇到歧义错误。
The problem seems to be that you have declared
ie, all three args must be either
float
orunsigned
. If you try to call it with other types (such asint
ordouble
), its ambiguous -- the compiler doesn't know which you want as both are just a good (or as bad if you prefer). You could improve things a bit by declaring more overloads:but you'd still get ambiguity errors if try to call it with mixed types.
浮点文字的类型是
double
,而不是float
。一般来说,您应该使用double
,除非您有使用float
的特定原因,例如需要消耗更少的内存。正如其他答案所提到的,您似乎已经声明:这意味着调用例如
Color(int, int, int)
有两种可能的转换,这两种转换都不是首选。您可以通过声明:相反,并在类本身内执行所需的任何转换,或调用构造函数来解决此问题:
The type of a floating-point literal is
double
, notfloat
. In general, you should be usingdouble
unless you have a specific reason for usingfloat
, such as needing to consume less memory. As other answers have mentioned, you seem to have declared:Which means that invoking, e.g.,
Color(int, int, int)
has two possible conversions, neither of which is preferred. You can fix this by declaring:Instead, and performing any conversions you need within the class itself, or invoking the constructor as:
考虑到您的测试用例,特别是
Color(r,g,b)
,我敢打赌您没有Color(int, int, int)
,但是颜色(无符号整数,无符号整数,无符号整数)
。这就是为什么您会收到意想不到的模糊电话。Given your test cases, particularly
Color(r,g,b)
, I'd bet you don't haveColor(int, int, int)
, butColor(unsigned int, unsigned int, unsigned int)
. That's why you're getting ambiguous calls that you're not expecting.取决于语言,但如果您使用的是 C# 或 C++,您可能可以转换它们。
例如对于 C#:
颜色((int)r, (int)g, (int)b)
或
颜色((浮点)r,(浮点)g,(浮点)b)
Depending on the language, but you can probably cast them, if you are using C# or C++.
e.g. for C#:
Color((int)r, (int)g, (int)b)
or
Color((float)r, (float)g, (float)b)