【问题标题】:glm overload operator for use in map dont work用于地图的 glm 重载运算符不起作用
【发布时间】:2018-04-24 19:12:00
【问题描述】:

我尝试使用 glm::vec2 作为 std::map 中的键,但我有这个错误:

严重性代码描述项目文件行抑制状态 错误 C2678 二进制“

在谷歌上搜索后发现我需要重载运算符,我尝试使用它:

bool operator<(const glm::vec2& lhs, const glm::vec2& rhs)
{
    return lhs.x + lhs.y < rhs.x + rhs.y;
}

那是我的工具.h

#pragma once
#include <gl/glew.h>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>

#include <string>
#include <fstream>
#include <iostream>
#include <math.h>


#include <..\glm/glm.hpp>
#include <..\glm/vec4.hpp>
#include <..\glm/mat4x4.hpp>
#include <..\glm/trigonometric.hpp>
// Include GLM extensions
#include <..\glm/gtc/matrix_transform.hpp>
#include <..\glm/gtc/type_ptr.hpp>
#include <..\glm/gtc/matrix_transform.hpp>
using namespace glm;
using namespace std;
bool operator<(const glm::vec2& lhs, const glm::vec2& rhs);
#include <map>

但它不能解决我的错误,我怎样才能重载运算符?

【问题讨论】:

  • 嗯?什么是“附加费运营商”?
  • 我想说超载
  • 您是否在声明std::map 之前包含的头文件中声明了您的operator&lt;()?此外,您可能希望从函数中删除 || lhs.y == rhs.y 部分。
  • 我已经编辑了我的问题,以便向您展示我的标题
  • v &lt; v 应该始终为假。你的功能会让它成为现实。

标签: c++ std glm-math


【解决方案1】:

我发现我只需要在好的命名空间中使用重载,在我的标题中我需要使用:

namespace glm {
    bool operator <(const glm::vec2& l, const glm::vec2& r);
    bool operator ==(const glm::vec2& l, const glm::vec2& r);
}

谢谢你的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多