为什么最后一个推回向量的对象的字段会转移到向量的其他对象?

Why do the fields of the last pushed back object into a vector transfer to the other objects of the vector?

本文关键字:向量 对象 转移 其他 字段 最后一个 为什么      更新时间:2023-10-16

我有点坚持我的代码,我无法找出问题所在。我希望你能帮助我。

我有4节课: 类 BasicModul:它有一个名为 modulName 的字段。 - 类绘图设置:此时不相关 类 ModulRepresentation:使用 BasicModul 对象和 DrawingSettings 对象构造。 - 类模块容器:它有一个字段,它是一个 std::vector 。代码中使用的对象是"容器"。

我的问题是,显然当将新的模表示推回模块容器(模表示的向量(时,似乎最后一个创建和推回的模表示的字段被传递到之前创建并推回模容器中的模表示的字段中。ModulContainer"容器"在代码的开头初始化。我正在使用ImGui来创建GUI。

错误在哪里?我以前有一个更简单的代码,它做的基本相同但封装较少(因此,例如,std::vector 直接在主代码中定义等等(,并且它按预期工作。modulCounter 变量从值 0 开始。我认为类函数的名称应该具有足够的描述性,但如果您需要更多信息,请告诉我。

提前感谢!

if (ImGui::Button("MODUL")){                // Buttons return true when clicked
modulCounter++;
int auxiliar=modulCounter*10;
std::string saux = std::to_string(auxiliar);
std::cout << "Here 1" << std::endl;         
BasicModul modAux(saux);
ModulDrawingSettings modDrawSet;
ModulRepresentation modRep(modAux, modDrawSet);
container.push_backModul(modRep);   
std::cout << saux << std::endl;
std::cout << "Vector Size/Capacity: " << container.modulesContainerSize() <<  "/" << container.modulesContainerCapacity() << std::endl;

std::cout << "Here 2" << std::endl;

for (int j=0; j<container.modulesContainerSize(); j++){
BasicModul modAuxiliar = container.getModulRepresentation(j).getBasicModul();
std::cout << "n" << std::endl;
std::cout << "Position in the container" << j << std::endl;
std::cout << container.getPointerToModulRepresentation(j) << std::endl;
std::cout << "name of the modul" << container.getModulRepresentation(j).getBasicModul().getModulName() << std::endl;
}
std::cout << "Here 3" << std::endl;

这是输出:

Here 1
Address of the original modul object: 0x7ffd6ba4cf30
10
Vector Size/Capacity: 1/1
name of the last modul passed: 10
Here 2

Position in the container0
0x5595967583c0
name of the modul10
Here 3
Here 1
Address of the original modul object: 0x7ffd6ba4cf30
20
Vector Size/Capacity: 2/2
name of the last modul passed: 20
Here 2

Position in the container0
0x559596b4c600
name of the modul20

Position in the container1
0x559596b4c720
name of the modul20
Here 3
Here 1
Address of the original modul object: 0x7ffd6ba4cf30
30
Vector Size/Capacity: 3/4
name of the last modul passed: 30
Here 2

Position in the container0
0x559596bb99b0
name of the modul30

Position in the container1
0x559596bb9ad0
name of the modul30

Position in the container2
0x559596bb9bf0
name of the modul30
Here 3
Here 1
Address of the original modul object: 0x7ffd6ba4cf30
40
Vector Size/Capacity: 4/4
name of the last modul passed: 40
Here 2

Position in the container0
0x559596bb99b0
name of the modul40

Position in the container1
0x559596bb9ad0
name of the modul40

Position in the container2
0x559596bb9bf0
name of the modul40

Position in the container3
0x559596bb9d10
name of the modul40
Here 3

所以我终于发现了错误。正如 @stark 和后来的 @M.M 所建议的那样,问题在于字段是一个指针,因此始终指向使用向量中最后一个推送对象更新的同一地址。由于它是一个指针,因此浅拷贝(在推回向量期间发生(是不够的。我只是通过使该字段只是我最初想要的"普通"类型而不是指针来修复它。

感谢您的回答!