Color(int, int, int) 与 Color(float, float, float) 不明确调用

发布于 2024-09-27 11:29:32 字数 570 浏览 0 评论 0原文

如何解决 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 技术交流群。

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

发布评论

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

评论(4

深空失忆 2024-10-04 11:29:32

问题似乎是您已经声明了

Color(unsigned, unsigned, unsigned);
Color(float, float, float);

即,所有三个参数都必须是 floatunsigned。如果您尝试使用其他类型(例如 intdouble)来调用它,它是不明确的——编译器不知道您想要哪个,因为两者都只是一个好的类型。 (或者如果你愿意的话也可以很糟糕)。您可以通过声明更多重载来改进一些事情:

Color(int, int, int);
Color(double, double, double);

但是如果尝试使用混合类型调用它,您仍然会遇到歧义错误。

The problem seems to be that you have declared

Color(unsigned, unsigned, unsigned);
Color(float, float, float);

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.

淑女气质 2024-10-04 11:29:32

浮点文字的类型是double,而不是float。一般来说,您应该使用double,除非您有使用float的特定原因,例如需要消耗更少的内存。正如其他答案所提到的,您似乎已经声明:

Color(unsigned int, unsigned int, unsigned int);
Color(float, float, float);

这意味着调用例如 Color(int, int, int) 有两种可能的转换,这两种转换都不是首选。您可以通过声明:

Color(int, int, int);
Color(double, double, double);

相反,并在类本身内执行所需的任何转换,或调用构造函数来解决此问题:

Color((unsigned int)r, (unsigned int)g, (unsigned int)b);

The type of a floating-point literal is double, not float. In general, you should be using double unless you have a specific reason for using float, such as needing to consume less memory. As other answers have mentioned, you seem to have declared:

Color(unsigned int, unsigned int, unsigned int);
Color(float, float, float);

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:

Color(int, int, int);
Color(double, double, double);

Instead, and performing any conversions you need within the class itself, or invoking the constructor as:

Color((unsigned int)r, (unsigned int)g, (unsigned int)b);
青衫儰鉨ミ守葔 2024-10-04 11:29:32

考虑到您的测试用例,特别是 Color(r,g,b),我敢打赌您没有 Color(int, int, int),但是 颜色(无符号整数,无符号整数,无符号整数)。这就是为什么您会收到意想不到的模糊电话。

Given your test cases, particularly Color(r,g,b), I'd bet you don't have Color(int, int, int), but Color(unsigned int, unsigned int, unsigned int). That's why you're getting ambiguous calls that you're not expecting.

半边脸i 2024-10-04 11:29:32

取决于语言,但如果您使用的是 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)

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