[] operator in std::map is giving me segmentation fault [closed]

99封情书 提交于 2019-12-13 11:19:52

问题


I have a

std::map<std::string, myClass*> myMap

then I am inserting like follow:

if(!myKey.empty())
{
    myMap[myKey] = this;
}

This sometime is throwing a segmentation fault.

Why??


回答1:


Maybe your myMap is no longer accessible. For instance, it might be a reference to a deleted pointer, or, much more probable, a member variable of an already deleted class:

class MyClass {
  public:
    selfInsert(std::string myKey) {
      if(!myKey.empty()) {
        myMap[myKey] = this;
      }
    }

  private:
    std::map<std::string, myClass*> myMap;
}

int main()
{
  MyClass *a = new MyClass();
  delete a;
  a->selfInsert();
}


来源:https://stackoverflow.com/questions/11252877/operator-in-stdmap-is-giving-me-segmentation-fault

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