【问题标题】:Failed to overload glm::vec2 '<' operator无法重载 glm::vec2 '<' 运算符
【发布时间】:2015-03-22 18:18:51
【问题描述】:

我需要将 std::map 与 glm::vec2 一起使用,所以我尝试实现 '

这是我的测试示例中的代码:

bool operator <(const glm::vec2& l, const glm::vec2& r)
{
    int lsize = l.x + l.y;
    int rsize = r.x + r.y;

    return lsize < rsize;
}


class A
{
public:
    A()
    {
        test[glm::vec2(5, 5)] = 4;
    }
private:
    std::map<glm::vec2, int> test;
};


int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    return 0;
}

当我创建自己的 Vector2 类并在那里以相同的方式实现运算符时,它会编译,但由于 glm::vec2 失败(我有 19 个错误告诉运算符 '

error C2676: binary '<' : 'const glm::vec2' does not define this operator or a conversion to a type acceptable to the predefined operator   

请注意,我的运算符重载函数编译,错误来自使用 std::map,似乎'

如果可以帮助您,这里是 GLM 资源:https://github.com/g-truc/glm/tree/master/glm

【问题讨论】:

    标签: c++ operator-overloading operators overloading glm-math


    【解决方案1】:

    令我担心的是,您甚至一开始就尝试这样做,因为我认为这可能不是您真正想要做的。但是如果你真的想这样做,你需要把它放在正确的命名空间中。

    namespace glm {
    namespace detail {
    // But don't do this!  This is not a good idea...
    bool operator <(const glm::vec2& l, const glm::vec2& r)
    {
        int lsize = l.x + l.y;
        int rsize = r.x + r.y;
        return lsize < rsize;
    }
    }
    }
    

    但是等等!这是错误的!

    • vec2 没有排序,因此实现 &lt; 在数学上没有任何意义
    • 重写库类型的运算符不是一个好主意
    • vec2 没有被订购,所以你不应该把它们放在 std::map

    std::map 使其中的元素保持有序。那么,当你这样做时会发生什么?

    int main(int argc, char* argv[])
    {
        std::map<glm::vec2, int> m;
        m[glm::vec2(1, 1)] = 10;
        std::cout << m[glm::vec2(0, 2)] << '\n';
        return 0;
    }
    

    是的,这会打印出 10,即使我们从未将 (0, 2) 添加到地图中。

    您可能希望使用空间索引(例如,quatdree)或至少使用字典顺序。除此之外,如果这是您真正想要的行为,您应该通过模板参数更改 std::map 上的比较器。

    struct vec2_cmp {
        bool operator()(const glm::vec2 &x, const glm::vec2 &y) { ... }
    }
    
    std::map<glm::vec2, int, vec2_cmp> m;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多