Comment surcharger correctement <opérateur dans un struct pour permettre une utilisation dans un ensemble en C ++

vasiliscsc:

Je suis en train de créer un ensemble de struct en C ++, une action nécessitant de surcharger le « < » opérateur.

Même si j'ai trouvé un moyen de faire la compilation, il ne soit pas encapsulé dans le struct, ce qui rend donc l'air impur pour la conception orientée objet.

Ce que j'ai essayé ce qui fonctionne:

#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 });
}

Ce que je voudrais faire quelque chose comme ceci:

#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 });
}

Cependant ce dernier ne compile pas.

Message d'erreur:

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

Je suis nouveau à C ++ et je me demandais s'il y a un moyen de faire quelque chose comme ça.

cigien:

Avez-vous regardé l'erreur de compilation?

Comme il est dit, la méthode doit être const

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

Je suppose que tu aimes

Origine http://10.200.1.11:23101/article/api/json?id=376950&siteId=1
conseillé
Classement