颜色(int,int,int)和颜色(float,float,float)模糊调用 [英] Color(int, int, int) vs Color(float, float, float) ambiguous call
问题描述
如何解决C ++中这两者之间的模糊调用?
How can I resolve the ambiguous call between these two in C++?
Color(int,int,int)
Color(float,float,float)
这些值是硬编码的,即 Color(1,2,3)
,当它们是变量 Color(r,g,b)
。为什么编译器不会根据数据类型解析?在变量形式?
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?
编辑:
对不起,太多的C ++让我忘记有其他语言。
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
推荐答案
问题似乎是
Color(unsigned, unsigned, unsigned);
Color(float, float, float);
即,所有三个参数必须为 float
或 unsigned
。如果你尝试用其他类型(如 int
或 double
)调用它,不知道你想要什么,因为两者只是一个好的(或如果你喜欢坏)。你可以通过声明更多的重载来改进一些东西:
ie, all three args must be either float
or unsigned
. If you try to call it with other types (such as int
or double
), 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:
Color(int, int, int);
Color(double, double, double);
但是如果尝试用混合类型调用它,您仍然会遇到模糊错误。
but you'd still get ambiguity errors if try to call it with mixed types.
这篇关于颜色(int,int,int)和颜色(float,float,float)模糊调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!