0

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. 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. However, now I am completely stumped.

The construction of the Row is simple enough:

//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):

//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). So, I need to index TwoDArray like TwoDArray[i], but as soon as I try to do that then I get this error:

"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. intLength is increasing because I technically need a jagged array which has got different sizes in each array.

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.

5
  • 2
    To begin with, please don't use pointers and new[] for arrays. If you want an "array" allocated at runtime use std::vector, if you want an array with a fixed size at compiletime use std::array. Commented Sep 28, 2017 at 12:15
  • Then for your problem, if you want to continue using pointers and new[]: You should use new[] in Array as well. With new Row(intLength) you allocate a single Row object. And you should not do your allocation in a loop. Commented Sep 28, 2017 at 12:16
  • @Someprogrammerdude I have to use arrays and pointers because that is what the project is about. The reason I called new Row(intLength) is because I have a constructor in Row with those parameters that creates a single array of integers. Which is why I want to call that multiple times (as many as intCols) Commented Sep 28, 2017 at 12:23
  • "I need to index TwoDArray like TwoDArray[i], but as soon as I try to do that..." which exact peace of code causes the error? Commented Sep 28, 2017 at 12:23
  • @AlexeyVoytenko where I try and call it like that in the for loop: for(int r=0; r<NumOfCol; r++) { //get the error here: TwoDArray[r] = new Row(intLength); } Commented Sep 28, 2017 at 12:24

1 Answer 1

1

With your loop in Array you allocate a single Row object multiple times, overwriting the pointer in each loop. That leads to a memory leak as only the last will be available through the variable 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.

The problem is that you can't do the array allocation using new[] at the same time as you call a specific constructor. You can not do e.g.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes a lot of sense. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.