STL Container as private member. Segmentation Fault
I'm having a strange problem with a STL Container.
I have a class with a private member std::map<string, string> _environment
. Why when I call _environment["name"]="john"
in the class constructor (or anywhere), I get a Segmentation fault
?
It should be the most common use of a STL container, shouldn't it?
Thanks!
Edit (more code):
In shell.h:
#include <string>
#include <map>
using namespace std;
class Shell {
public:
Shell();
Shell(const Shell& orig){};
virtual ~Shell(){};
private:
...
...
std::map<string, string> _environment;
};
In shell.cpp:
Shell::Shell() {
_environment["shell"] = "myshell";
...
}
开发者_C百科
The segmentation fault occurs in the line _environment["shell"] = "myshell";
There is no reason for Segmentation fault. And for simplify inserting I recommend to use boost::assign library, like this:
Shell::Shell() {
using namespace boost::assign;
insert( _environment )( "shell", "myshell" );
...
}
This is more elegantly and more effectively.
精彩评论