lundi 1 octobre 2018

C++ Assign std::map values with enum class object

Consider following code. In my real case scenario i have somthing like that:

typedef enum
{
    vehicle,
    computer,

} Article;

And that is what I'm trying to construct:

enum class status{
    notPaid,
    paid,
};


struct S {
    status status_vehicle;
    status status_computer;

    std::map<Article, status> mymap =
    {
        {vehicle,  S::status_vehicle},
        {computer, S::status_computer},
    };
};

int main ()
{   
    Article a1 = vehicle;
    S::mymap.at(a1) = status::paid; // this line doesn't work
}

However, the last line (S::mymap.at(a1) = status::paid;) is not working. I've tried different approaches, using the find() function of std::map for example. I got the error "assignment of member std::pair<Article, status>::second in read only object".

Does someone know, how to do that? Also maybe how to design the whole in a better way? (the whole from the line "And that is what I'm trying to construct"). Also I would have prefer to use an unordered_map instead of a map but was not working. Thanks

Aucun commentaire:

Enregistrer un commentaire