Unable to access non-const member functions of objects in C++ std::set

后端 未结 3 1867
后悔当初
后悔当初 2020-12-07 04:32

Message is a class I made. I have a set of them in the main function that I pass to messageTimeOut (and some other functions). In messageTimeOut using an itorator I am loopi

3条回答
  •  失恋的感觉
    2020-12-07 05:02

    You can't.

    Elements inside an std::set are always constant, because otherwise an user could modifiy the ordering in the set by modifying an element which belong to the set.

    Notice that it doesn't matter that the function that you invoke doesn't change the ordering, since std::set has no way to check this.

    A common way to fix this is to remove the element from the set, modify it, then reinsert it. Another way would be to use a map, even if this would probably force you to duplicate some data.

提交回复
热议问题