简体   繁体   English

在不同的类C ++中构造类属性数组

[英]Constructing an array of class attribute in a different class C++

I have to do a project where one class Row has an array of integers int* OneArray and then another class Array has an array of the first class Row* TwoDArray . 我必须做一个项目,其中一个类Row具有一个整数数组int* OneArray ,然后另一个类Array具有一个第一类Row* TwoDArray In essence the class has a 2D array on integers, and I can easily do the construction of the 2D array when it is in one class. 本质上讲,该类在整数上具有2D数组,并且在一个类中时,我可以轻松地构造2D数组。 However, now I am completely stumped. 但是,现在我完全陷入了困境。

The construction of the Row is simple enough: Row的构造非常简单:

//set length of array
numOfRows = intRows;
//Create space for array
OneArray = new int[intRows];
//populate array with random numbers
for(int i=0; i<intRows; i++)
{
    OneArray[i] = GenerateRandom(9,0);
}

This is where I am stuck (Construction of Array ): 这就是我卡住的地方( Array构造):

//Set Number of Cols
NumOfCol = intCols;
//create length for each row
int intLength = 4;
for(int i=0; i<NumOfCol; i++)
{
    //create space and call row constructor with length
    TwoDArray = new Row(intLength);
    //increase length for jagged array
    intLength++;
}

As it is now it writes over the current array after each for loop (which is expected). 现在,它在每个for循环之后覆盖当前数组(这是预期的)。 So, I need to index TwoDArray like TwoDArray[i] , but as soon as I try to do that then I get this error: 因此,我需要像TwoDArray[i]那样索引TwoDArray ,但是一旦尝试这样做,就会出现此错误:

"invalid user-defined conversion from 'Row*' to 'const Row&'." “从'行*'到'常量行&'的无效的用户定义转换。”

Note: If I take the line out of the for loop only the first array is made and not until intCol . 注意:如果我intCol行从for循环中删除,则仅创建第一个数组,直到intCol为止。 intLength is increasing because I technically need a jagged array which has got different sizes in each array. intLength在增加,因为从技术上讲,我需要一个锯齿状的数组,每个数组中的大小都不同。

My classes look like this: 我的课程如下所示:

    class Row
{
    public:
        //Constructors
        Row();
        Row(int intRows);
        Row(const Row& objPrev);

        //Accessors
        int getNumOfRows();
        int getRowArray(int intRow);

        //Mutators
        void setRowArray(int intRow, int intChange);

        //Destructor
        ~Row();
    private:
        int* OneArray;
        int numOfRows;
}

and

 class Array
{
    public:
        //Constructors
        Array();
        Array(int intRows, int intCols);
        Array(const Array& objPrev);

        //Accessors
        int getNumOfCol();
        Row getTwoDArray(int intCol, int intRow);

        //Mutators
        void setTwoDArray(int intCol, int intRow, int intChageTo);

        //Destructor
        ~Array();
   private:
        Row* TwoDArray;
        int NumOfCol;
}

Any Help or suggestions are appreciated. 任何帮助或建议表示赞赏。

With your loop in Array you allocate a single Row object multiple times, overwriting the pointer in each loop. 使用Array的循环,您可以多次分配一个Row对象,从而覆盖每个循环中的指针。 That leads to a memory leak as only the last will be available through the variable TwoDArray . 这导致内存泄漏,因为只有最后一个变量可通过变量TwoDArray Also, at the end of the loop all you will have is an "array" of only a single element, the last allocated Row object. 同样,在循环结束时,您将只有一个元素的“数组”,即最后分配的Row对象。

The problem is that you can't do the array allocation using new[] at the same time as you call a specific constructor. 问题是您不能在调用特定构造函数的同时使用new[]进行数组分配。 You can not do eg 不能做例如

TwoDArray = new Row[NumOfCol](intLength);

Instead you have to split the allocation and initialization into two parts: 相反,您必须将分配和初始化分为两部分:

TwoDArray = new Row[NumOfCol];  // Allocates memory, default constructed Row objects

for (int i = 0; i < NumOfCol; ++i)
{
    TwoDArray[i] = Row(intLength);  // Initialize each element
}

This of course requires you follow the rules of three or five (or zero) for the copying in the loop to work. 当然,这需要您遵循三或五个(或零)规则,才能在循环中进行复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM