动态数组成员变量赋值

C++ Dynamic Array Member Variable Assignment

本文关键字:赋值 变量 组成员 数组 动态      更新时间:2023-10-16

我有一个问题,分配新的值给一个动态int数组,这是类IntersectionFlowRate()的数据成员变量。我可以在构造函数中初始化和打印数组的值。但是,当我退出构造函数到另一个类,然后稍后调用IntersectionFlowRate()类中的函数传递变量来覆盖数据成员的初始值时,它将分段错误。我进行了调试,发现重写数组会导致segfault。即使试图访问动态数组中的一个函数也会出现故障。

我的问题是如何编辑一个动态int数组成员变量的值从它的函数之一,即setArrayElement(int index, int x)。

下面是我的一些代码。抱歉,如果我不清楚或错过了一些荒谬的东西。这件事我已经纠结了好几个小时了。
    #ifndef INTERSECTIONFLOWRATE_H
    #define INTERSECTIONFLOWRATE_H
    class IntersectionFlowRate
    {
    public:
        IntersectionFlowRate();
        ~IntersectionFlowRate();
        void setFlowCycle(int index, int flow);
    private:
        int* m_flowRateMotorCycle;
 };
 #endif
^

    #include "IntersectionFlowRate.h"
    #include <cstdlib>
    #include <iostream>
    #include <new>
    using namespace std;
    IntersectionFlowRate::IntersectionFlowRate()
    {
        const int SIZE = 4; //Constant for m_flowRates[] size
        //DYNAMIC MEMORY DELETE LATER
        m_flowRateMotorCycle = new int[SIZE];
        for(int i = 0; i < SIZE; i++){
            m_flowRateMotorCycle[i] = 0;
            cout << m_flowRateMotorCycle[i] << endl;
            cout << "WE GOT HERE" << endl;
        }
    }
    void IntersectionFlowRate::setFlowCycle(int index, int flow){
        cout << "INDEX: " << index << endl;
        cout << "FLOW: " << flow << endl;
        m_flowRateMotorCycle[index] = flow; //seg fault is here
    }

我有另一个类,创建一个指向IntersectionFlowRate()对象的指针,然后调用它的setFlowCycle函数传递两个VALID int。通过调试,我能够将0和3传递给函数setFlowCycle(0,3),并在函数中输出这些变量。

    #ifndef TRAFFICSIM_H
    #define TRAFFICSIM_H
    #include "IntersectionFlowRate.h"
    using namespace std;
    class TrafficSim
    {
    public:
        TrafficSim(); //Default Constructor
        TrafficSim(const char* file); //Constructor
        ~TrafficSim(); //Destructor

    private:
        IntersectionFlowRate* m_flowRate;
    };
    #endif

    #include "TrafficSim.h"
    #include "IntersectionFlowRate.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>

    using namespace std;
    TrafficSim::TrafficSim()
    {
        IntersectionFlowRate* m_flowRate = new IntersectionFlowRate();
        m_flowRate->setFlowCycle(0, 3);
    }
我用这段代码重复了这个错误。如果没有其他人可以,我完全不确定哪里可能出了问题。

您正在设置一个名为m_flowRate的局部变量,而不是TrafficSim类的成员变量m_flowRate:

而不是:

TrafficSim::TrafficSim()
{
    IntersectionFlowRate* m_flowRate = new IntersectionFlowRate();
    m_flowRate->setFlowCycle(0, 3);
}

应该是这样的:

 TrafficSim::TrafficSim()
 {
    m_flowRate = new IntersectionFlowRate();
    m_flowRate->setFlowCycle(0, 3);
 }

但总的来说,它不需要是指针。它可以是类中的对象成员。这将减少一点指针的使用:

class TrafficSim
{
    public:
        TrafficSim(); //Default Constructor
        TrafficSim(const char* file); //Constructor
    private:
        IntersectionFlowRate m_flowRate;
};

:

TrafficSim::TrafficSim()
{
    m_flowRate.setFlowCycle(0, 3);
}

关于如何在类中合并使用std::vector的问题,这里是IntersectionFlowRate类的代码示例,使用vector重写:

向量样本

同样,问题的另一个来源是,当您在类中有指向动态分配内存的指针时,您的类不能遵循规则3。

使用std::vector自动处理此问题,但如果您坚持使用指针,则需要遵守所发布的链接中的说明。

是的,使用std::vector,它更简单,它是一个模板,所以它也非常快,可以工作任何类型(最适合原始类型或对象指针),它也有边界检查和其他有用的东西。

如果你需要快速的类似数组的访问,那么你可以使用std::map,它将键与值相关联,就像这样

    std::map<UINT, YourClass*> m_mapIDs_to_YourClass;

当你第一次开始使用stl容器时,它们可能看起来有点奇怪,但不久之后你就离不开它们了,幸运的是,它们现在已经成为c++标准的一部分有一段时间了。

这两个容器的边界检查可以通过比较你的迭代器和mapYourMap.end()来完成,如果它们相等,你已经传递了最后一个元素,并且试图通过迭代器访问数据将导致异常。std::vector的例子(如果vecInt是一个vector):

    vector<int>::iterator it = vecInt.begind();
    if (it == vecInt.end()) return; // vector is empty
    do {  // runs through elememts until out of bound, useful for searching
        i++
    while (it != vecInt.end());