C++ c++;结构作为映射键和运算符重载

C++ c++;结构作为映射键和运算符重载,c++,c++11,C++,C++11,我正在尝试跟踪地图或集合中的二维坐标 以下代码导致此错误: 错误C2678:二进制'重载运算符作为常量函数: #include <iostream> #include <map> struct Coord_T { uint64_t x, y; inline bool operator==(const Coord_T& o) const { return x == o.x && y == o.y; } inline b

我正在尝试跟踪地图或集合中的二维坐标

以下代码导致此错误:
错误C2678:二进制'重载运算符作为常量函数:

#include <iostream>
#include <map>



struct Coord_T {
    uint64_t x, y; 
    inline bool operator==(const Coord_T& o) const { return x == o.x && y == o.y; }
    inline bool operator<(const Coord_T& o) const { return x < o.x || (x == o.x && y < o.y); }
    inline bool operator>(const Coord_T& o) const { return x > o.x || (x == o.x && y > o.y); }
    inline bool operator!=(const Coord_T& o) const { return x != o.x || y != o.y; }
    inline bool operator<=(const Coord_T& o) const { return x < o.x || (x == o.x && y <= o.y); }
    inline bool operator>=(const Coord_T& o) const { return x > o.x || (x == o.x && y >= o.y); }

};


int main()
{
    Coord_T coord;
    coord.x = 5; 
    coord.y = 6; 
    std::map<Coord_T, bool> vals;
    vals[coord] = true;
    return 0; 
}
#包括
#包括
结构协调{
uint64_t x,y;
内联布尔运算符==(const Coord_T&o)const{return x==o.x&&y==o.y;}
内联布尔运算符(const-Coord|T&o)const{return x>o.x | |(x==o.x&&y>o.y);}
内联布尔运算符!=(常数坐标T&o)常数{return x!=o.x | | y!=o.y;}
内联布尔运算符o.x | |(x==o.x&&y>=o.y);}
};
int main()
{
协调;
坐标x=5;
坐标y=6;
标准:map VAL;
VAL[coord]=真;
返回0;
}

重载运算符作为常量函数:

#include <iostream>
#include <map>



struct Coord_T {
    uint64_t x, y; 
    inline bool operator==(const Coord_T& o) const { return x == o.x && y == o.y; }
    inline bool operator<(const Coord_T& o) const { return x < o.x || (x == o.x && y < o.y); }
    inline bool operator>(const Coord_T& o) const { return x > o.x || (x == o.x && y > o.y); }
    inline bool operator!=(const Coord_T& o) const { return x != o.x || y != o.y; }
    inline bool operator<=(const Coord_T& o) const { return x < o.x || (x == o.x && y <= o.y); }
    inline bool operator>=(const Coord_T& o) const { return x > o.x || (x == o.x && y >= o.y); }

};


int main()
{
    Coord_T coord;
    coord.x = 5; 
    coord.y = 6; 
    std::map<Coord_T, bool> vals;
    vals[coord] = true;
    return 0; 
}
#包括
#包括
结构协调{
uint64_t x,y;
内联布尔运算符==(const Coord_T&o)const{return x==o.x&&y==o.y;}
内联布尔运算符(const-Coord|T&o)const{return x>o.x | |(x==o.x&&y>o.y);}
内联布尔运算符!=(常数坐标T&o)常数{return x!=o.x | | y!=o.y;}
内联布尔运算符o.x | |(x==o.x&&y>=o.y);}
};
int main()
{
协调;
坐标x=5;
坐标y=6;
标准:map VAL;
VAL[coord]=真;
返回0;
}

似乎是。你在用什么编译器?你应该让所有的操作符都是代码> const 。我使用VisualStudio社区和它的默认C++编译器。你在用什么编译器?你应该让所有的操作符都是代码> const 。我使用VisualStudio社区和它的默认C++编译器。