どのように適切にCでのセットでの使用を可能にする構造体で<演算子をオーバーロードする++

vasiliscsc:

私はC ++、「<」演算子をオーバーロードするために必要なアクションで構造体のセットを作成しようとしています。

私はそれをコンパイルようにする方法を見つけたとしても、それゆえ、それはオブジェクト指向設計のために汚れた見えるように、構造体の中にカプセル化されません。

私は働く何を試してみました:

#include <iostream>
#include <set>

struct Coordinate {
    int x, y;
};

bool operator<(const Coordinate& l, const Coordinate& r) {
    return l.x * l.y < r.x * r.y;
}

int main() {
    std::set<Coordinate> mySet;
    mySet.insert(Coordinate{ 5,5 });
}

私は何をしたいのは、このようなものです:

#include <iostream>
#include <set>

struct Coordinate {
    int x, y;

    bool operator<(const Coordinate& other) {
        return this->x * this->y < other.x * other.y;
    }
};

int main() {
    std::set<Coordinate> mySet;
    mySet.insert(Coordinate{ 5,5 });
}

しかし、後者はコンパイルされません。

エラーメッセージ:

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xstddef(127,1): error C2678: binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)
1>        with
1>        [
1>            _Ty=Coordinate
1>        ]
1>C:\CppWorkspace\Dungeons of Doom\Dungeons of Doom\src\TestMain.cpp(7,7): message : could be 'bool Coordinate::operator <(const Coordinate &)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xstddef(127,1): message : while trying to match the argument list '(const _Ty, const _Ty)'
1>        with
1>        [
1>            _Ty=Coordinate
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xstddef(126): message : while compiling class template member function 'bool std::less<Coordinate>::operator ()(const _Ty &,const _Ty &) const'
1>        with
1>        [
1>            _Ty=Coordinate
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xutility(1469): message : see reference to function template instantiation 'bool std::less<Coordinate>::operator ()(const _Ty &,const _Ty &) const' being compiled
1>        with
1>        [
1>            _Ty=Coordinate
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xmemory(1318): message : see reference to class template instantiation 'std::less<Coordinate>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xmemory(1318): message : see reference to variable template 'const bool is_empty_v<std::less<Coordinate> >' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\set(54): message : see reference to class template instantiation 'std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>' being compiled
1>        with
1>        [
1>            _Kty=Coordinate,
1>            _Pr=std::less<Coordinate>,
1>            _Alloc=std::allocator<Coordinate>
1>        ]
1>C:\CppWorkspace\Dungeons of Doom\Dungeons of Doom\src\TestMain.cpp(17): message : see reference to class template instantiation 'std::set<Coordinate,std::less<Coordinate>,std::allocator<Coordinate>>' being compiled

私はC ++に新しいですし、このような何かを行う方法があるかどうかを疑問に思いました。

cigien:

あなたは、コンパイルエラーを見ていましたか?

それが言うように、この方法は、constのである必要があります

bool operator<(const Coordinate& other)  const {   // const the < operator
        return this->x * this->y < other.x * other.y;
    }

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=376949&siteId=1