' Klocwork' profiller生成错误[' it.second.name _._ M_dataplus._M_p'可以在此函数中使用未初始化。]在以下代码部分中。
class Test {
public:
Test() { }
Test(std::string str) {
name = str;
cout <<"Test::Test object: " <<name <<endl;
}
~Test() {
cout <<"Test::~Test object: " <<name <<endl;
}
string getName() {
return name;
}
private:
string name;
};
class MapHandler {
private:
map<int, Test> myMap;
public:
void MapFiller();
void MapDisplay();
};
void MapHandler::MapFiller() {
Test obj1("Obj1");
Test obj2("Obj2");
Test obj3("Obj3");
myMap[1] = obj1;
myMap[2] = obj1;
myMap[3] = obj3;
}
void MapHandler::MapDisplay() {
map<int, Test> tmpMap;
for(auto it : myMap) {
cout <<"object: " << it.second.getName() <<endl;
tmpMap[it.first] = it.second;
} **//Error Line**
}
但是在使用[g ++ -std = c ++ 11 -Wall -Werror test.cpp]的简单编译中,它工作正常。
答案 0 :(得分:1)
尝试更改默认构造函数:
Test()
{
name = "";
}