1

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

我有 4 个类: - 类 BasicModul:除其他外,它有一个名为 modulName 的字段。- 类 DrawingSettings:此时不相关 - 类 ModulRepresentation:由 BasicModul 对象和 DrawingSettings 对象构成。- 类 ModulesContainer:它有一个字段是 std::vector 。它在代码中使用的对象是“容器”。

我的问题是,显然当将新的 ModulRepresentation 推回 ModulesContainer (ModulRepresentation 的向量)时,似乎最后创建并推回的 ModulRepresentation 的字段被传递到先前创建并推回 ModulContainer 中的 ModulRepresentation 的字段中. 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
4

1 回答 1

1

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

谢谢您的回答!

于 2019-12-26T00:55:44.010 回答