C++ 为什么运营商要<;被定义为非成员?

C++ 为什么运营商要<;被定义为非成员?,c++,map,hashmap,C++,Map,Hashmap,我在hash_映射上做了一些测试,使用struct作为键。我定义结构: struct ST { bool operator<(const ST &rfs) { return this->sk < rfs.sk; } int sk; }; 然后: stdext::hash\u映射; ST; 图.插入(标准::make_pair(st,3)); 它给了我一个编译器错误:二进制'您缺少一个常量: bool operato

我在hash_映射上做了一些测试,使用struct作为键。我定义结构:

struct ST
{

    bool operator<(const ST &rfs)
    {
        return this->sk < rfs.sk;
    }

    int sk;
};
然后:

stdext::hash\u映射;
ST;
图.插入(标准::make_pair(st,3));

它给了我一个编译器错误:二进制'您缺少一个
常量

bool operator<(const ST &rfs) const
{
    return this->sk < rfs.sk;
}
bool操作符sk
您缺少一个
常量:

bool operator<(const ST &rfs) const
{
    return this->sk < rfs.sk;
}
bool操作符sk
我认为问题在于


错误:二进制'我认为问题是


错误:二进制“尽管有上述答案,我建议您看看:

C++入门,第四版,第14章,第14.1节

通常我们将算术运算符和关系运算符定义为 非成员函数,我们将赋值运算符定义为成员:


尽管有上述答案,我还是建议您看看:

C++入门,第四版,第14章,第14.1节

通常我们将算术运算符和关系运算符定义为 非成员函数,我们将赋值运算符定义为成员:


是的,我检查错误抛出的位置。'bool operator()(const\u Ty&u Left,const\u Ty&u Right)const'。当然,我错过了常量。@MIKU\u LINK:是的,这是能够调用
const ST
对象上的运算符所必需的。是的,我检查错误抛出的位置。'bool operator()(const\u Ty&u Left,const\u Ty&u Right)const'。当然,我错过了常量。@MIKU_LINK:是的,这是能够调用
const ST
对象上的操作符所必需的。
bool operator<(const ST& lfs, const ST& rfs)
{
    return lfs.sk < rfs.sk;
}
bool operator<(const ST &rfs) const
{
    return this->sk < rfs.sk;
}