我得到一個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