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

问题描述 投票:1回答:1

我对我的代码有些困惑,我无法找出问题所在。我希望你能帮助我。

我有4个课程:-类BasicModul:除其他外,它具有一个称为modulName的字段。-类DrawingSettings:目前不相关-类ModulRepresentation:由BasicModul对象和DrawingSettings对象构造。-类ModulesContainer:它具有一个字段,该字段为std :: vector。代码中使用的对象是“容器”。

[我的问题是,当将新的ModulRepresentation推回ModulesContainer(ModulRepresentation的向量)时,似乎最后一个创建并推回的ModulRepresentation的字段传递到了先前创建并推回的ModulRepresentation的字段中。 ModulContainer。 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
c++ vector deep-copy shallow-copy imgui
1个回答
0
投票

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

谢谢您的回答!

© www.soinside.com 2019 - 2024. All rights reserved.