gpt4 book ai didi

linux - 错误 (Klocwork) : '_M_dataplus._M_p' might be used uninitialized in this function

转载 作者:太空狗 更新时间:2023-10-29 12:25:21 26 4
gpt4 key购买 nike

'Klocwork' profiller 在以下代码部分生成错误 ['it.second.name_._M_dataplus._M_p' might be used uninitialized in this function.]。

    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] 的简单编译中它工作正常。

最佳答案

尝试更改默认构造函数:

Test()
{
name = "";
}

关于linux - 错误 (Klocwork) : '_M_dataplus._M_p' might be used uninitialized in this function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43271158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com