std::set acting up on insert.

时光毁灭记忆、已成空白 提交于 2019-12-25 05:11:51

问题


This is likely very simple. I have added an std::set<int> my_set; to my header file for some class. Then, in that classes implementation, I try to insert into this set. As an example, just doing my_set.insert(1); This is not compiling, which is very strange behaviour. Here is my compiler error:

error C2663: 'std::_Tree<_Traits>::insert' : 4 overloads have no legal conversion for 'this' pointer

The file I included to use set is #include <set>. What have I done wrong? I have not called the set anywhere else in the code.


回答1:


Since you're getting an error about this, but you're trying to insert an int, I'm gonna go out on a limb here and guess you're trying to do this in a const method.

class A
{
    std::set<int> my_set;
    void foo() 
    {
       my_set.insert(1); //OK
    }
    void foo() const
    {
       my_set.insert(1); //not OK, you're trying to modify a member
                         //std::set::insert is not const, so you can't call it
    }
};

Remove the const. It seems like the logical thing to do, since you're modifying members.



来源:https://stackoverflow.com/questions/10642692/stdset-acting-up-on-insert

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!