dimanche 18 octobre 2020

Cannot insert struct into set: “no match for operator<�” [duplicate]

I was trying to learn to use sets that contain a struct, but ran into a strange problem.

Here is the code:

#include <cstring>
#include <iostream>
#include <string>
#include <map>
#include <set>

using namespace std;

struct Course {
    string name;
    string theme;
    int enrollments;
};



int main()
{

    Course test_course1 = {"English", "Language", 5};
    Course test_course2 = {"Spanish", "Language", 4};
    Course test_course3 = {"Spanish", "Language", 10};

    set<Course> test_set;

    // This is what causes the issues. If I block the following three lines of code, the program runs perfectly.
    test_set.insert(test_course1);
    test_set.insert(test_course2);
    test_set.insert(test_course3);


    map <string, set<Course>> test_map;
    test_map.insert({"London", test_set});
    test_map.insert({"New York", test_set});
    test_map.insert({"New York", test_set});


    for (auto i: test_map)
    {

        cout << "In  " << i.first << " there are following courses " << endl;

        for (auto x :i.second){
            cout <<x.name << endl;
        }

    }

    return 0;
}

The issue? I've take a screenshot of all the issues that appear if I try to insert the struct Course into a set: the screenshot

P.S. I know that that test_course2 and test_course3 are almost identical. I'm just testing...

Aucun commentaire:

Enregistrer un commentaire