我得到一个segmentation fault
试图用unique_ptr
创建派生类的实例时。 之前,我曾编码的七个子类的实例每次,一前一后的代码工作正常。赛格故障的unique_ptr与工厂设计尝试
当前的代码如下:
typedef std::unique_ptr<Comum> ComumPtr;
ComumPtr createInstance (string dom, map<string, string> & config, map<string, string> & config_fields)
{
ComumPtr ptr; // initialized to nullptr.
if (dom == "voice") {
ptr.reset (new Voice (config, config_fields));
// } else if (dom == "account") { // FOR OTHER DERIVED CLASSES
// ptr.reset (new Account (config, config_fields));
}
return ptr;
}
// At main function:
for (vector<string>::const_iterator cit = for_domain.begin(); cit != for_domain.end(); ++cit) {
const char * section (cit->c_str());
string fsn = *cit + "_fields";
const char * fields_section_name (fsn.c_str());
const char * db_section ("Database");
map <string, string> domain_config = cfg.getSectionConfig (config_file.c_str(), section);
map <string, string> domain_config_fields = cfg.getSectionConfig (config_file.c_str(), fields_section_name);
map <string, string> database_config = cfg.getSectionConfig (config_file.c_str(), db_section);
std::unique_ptr<Comum> domain = createInstance(*cit, domain_config, domain_config_fields);
domain->readDatabaseFields (database_config); // <- segmentation fault
你看不到任何原因赛格故障?
事实上,它指向NULL。忘记包括该支票。在createInstance中,在'main'中修改''dom =='voice''应该有'voice'大写。谢谢。 – Luis