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.
new[]for arrays. If you want an "array" allocated at runtime usestd::vector, if you want an array with a fixed size at compiletime usestd::array.new[]: You should usenew[]inArrayas well. Withnew Row(intLength)you allocate a singleRowobject. And you should not do your allocation in a loop.new Row(intLength)is because I have a constructor inRowwith those parameters that creates a single array of integers. Which is why I want to call that multiple times (as many asintCols)for(int r=0; r<NumOfCol; r++) { //get the error here: TwoDArray[r] = new Row(intLength); }