解决运算符歧义

发布于 2024-10-21 04:05:14 字数 690 浏览 3 评论 0原文

我有以下映射类型...

std::map<D3DXCOLOR, ID3DXMesh*>

在编译期间,xfunction 抱怨它无法解决有关键类型的歧义

error C2593: 'operator <' is ambiguous

编译器检测到的候选运算符如下;

  1. 内置 C++ 运算符<(DWORD, DWORD)
  2. 内置 C++ 运算符<(FLOAT, FLOAT)
  3. 内置 C++ 运算符<(D3DCOLORVALUE, D3DCOLORVALUE)

D3DXCOLOR 结构由 4 个浮点数组成 r, <分别是strong>g、ba,但没有定义运算符<。然而,它确实提供了 DWORD FLOAT 和 D3DCOLORVALUE 的转换函数,因此也提供了候选列表中的条目。

我正在考虑解决这个问题的最佳方法。我可以为 D3DXCOLOR 编写自己的内联运算符,将颜色包装在一个提供自己的运算符 < 的新类中,或者是否可以以某种方式提示编译器应从候选列表中选择哪个实现? DWORD 运算符 <会充分满足我的要求。

I have the following map type...

std::map<D3DXCOLOR, ID3DXMesh*>

During compilation, xfunctional complains that it cannot resolve an ambiguity regarding the key type;

error C2593: 'operator <' is ambiguous

The candidate operators detected by the compiler are as follows;

  1. built-in C++ operator<(DWORD, DWORD)
  2. built-in C++ operator<(FLOAT, FLOAT)
  3. built-in C++ operator<(D3DCOLORVALUE, D3DCOLORVALUE)

The D3DXCOLOR struct consists of 4 floats r, g, b, and a respectively but does not define a operator <. It does however provide cast functions for DWORD FLOAT and D3DCOLORVALUE, hence the entries in the candidate list.

I am contemplating the best way to resolve this problem. I could write my own inline operator for D3DXCOLOR, wrap the colour inside a new class which provides its own operator <, or is it possible to somehow hint to the compiler which implementation should be chosen from the list of candidates? The DWORD operator < would meet my requirements adequately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

指尖微凉心微凉 2024-10-28 04:05:14

你有三个选择。例如,假设您希望将它们作为颜色值进行比较:

1)定义operator<

bool operator<(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
    return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
}

2)专门化std :: less

namespace std {
    template <>
    struct less<D3DXCOLOR> {
        bool operator()(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
            return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
        }
    };
}

3)为您的地图提供第三个模板参数 -请注意,这会更改地图的类型,因此如果您经常传递地图,这可能会很不方便。但它表示该顺序仅用于此地图,而不是用于任何其他目的的规范“正确”颜色顺序。

struct mycomparator {
    bool operator()(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
        return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
    }    
};

std::map<D3DXCOLOR, ID3DXMesh*, mycomparator>

You have three options. Supposing for example that you want them compared as colorvalues:

1) Define operator<:

bool operator<(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
    return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
}

2) Specialize std::less:

namespace std {
    template <>
    struct less<D3DXCOLOR> {
        bool operator()(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
            return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
        }
    };
}

3) Supply a third template parameter to your map - note that this changes the type of the map, so if you pass the map around a lot this might be inconvenient. But it expresses that the ordering is to be used only for this map, not as the canonical "correct" order of colors for any other purpose.

struct mycomparator {
    bool operator()(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
        return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
    }    
};

std::map<D3DXCOLOR, ID3DXMesh*, mycomparator>
我只土不豪 2024-10-28 04:05:14

您只需将小于函子传递给应该使用的 map 类模板即可。

struct D3DXCOLOR_less {
    bool operator ()(D3DXCOLOR const&a, D3DXCOLOR const& b) const { … }
};

std::map<D3DXCOLOR, ID3DXMesh*, D3DXCOLOR_less> foo;

这绝对是我在这种情况下会做的事情,除非在其他情况下您还需要此类的运算符 <

You can just pass a less-than functor to the map class template that should be used.

struct D3DXCOLOR_less {
    bool operator ()(D3DXCOLOR const&a, D3DXCOLOR const& b) const { … }
};

std::map<D3DXCOLOR, ID3DXMesh*, D3DXCOLOR_less> foo;

This is definitely what I would do in this case, unless you also need the operator < for this class in other cases.

沫雨熙 2024-10-28 04:05:14

您需要编写自己的运算符<,或者为映射提供比较器函子。

struct CompareColor {
  bool operator()(D3DXCOLOR const & L, D3DXCOLOR const & R) const {
    // Compare and return whether L is less than R
  }
}

map<D3DXCOLOR, ID3DXMesh*, CompareColor> TheMap;

You'll need to write your own operator<, or provide a comparator functor to the map.

struct CompareColor {
  bool operator()(D3DXCOLOR const & L, D3DXCOLOR const & R) const {
    // Compare and return whether L is less than R
  }
}

map<D3DXCOLOR, ID3DXMesh*, CompareColor> TheMap;
酒浓于脸红 2024-10-28 04:05:14

D3DXCOLOR 定义 operator< 函数,为

bool operator<(const D3DXCOLOR &c1, const D3DXCOLOR &c2)
{
   return <some boolean value>;
}

或定义一个 compare 函子,称为 D3DXCOLOR_LESS 并将其作为第三个传递std::map 的参数:

struct D3DXCOLOR_LESS
{
    bool operator()(const D3DXCOLOR &c1, const D3DXCOLOR &c2)
    {
       return <some boolean value>;
    }
};

std::map<D3DXCOLOR, ID3DXMesh*, D3DXCOLOR_LESS>  colormap;

Define operator< function for D3DXCOLOR, as

bool operator<(const D3DXCOLOR &c1, const D3DXCOLOR &c2)
{
   return <some boolean value>;
}

Or define a compare functor, something called D3DXCOLOR_LESS and pass it as third parameter to the std::map:

struct D3DXCOLOR_LESS
{
    bool operator()(const D3DXCOLOR &c1, const D3DXCOLOR &c2)
    {
       return <some boolean value>;
    }
};

std::map<D3DXCOLOR, ID3DXMesh*, D3DXCOLOR_LESS>  colormap;
廻憶裏菂餘溫 2024-10-28 04:05:14

实际上,RGBA 颜色没有像任何标量那样的默认准顺序。并且您不应该在全局上下文中定义一个顺序,但您可以定义自己的顺序并在 std::map 模板实例中指定它。请参阅参数说明 http://www.sgi.com/tech/stl/Map.html

Actually RGBA color have no some default quasi-order like any scalar. And you shouldn't define one in global context, but you can define your own ordering and specify it in std::map template instance. See parameters description at http://www.sgi.com/tech/stl/Map.html

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