Creating 2D array in header

Ok, so I'm writing a matrix program that'll do basic function such as addition and multiplication. Right now, I'm creating the object matrix, and I'm having trouble creating a 2D array. The error is error: declaration of 'mtrix' as multidimensional array must have bounds for all dimensions except the first.
Here's the header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef MATRIX_H
#define MATRIX_H

class matrix
{
    public:
        matrix(int numRows, int numCols);
        virtual ~matrix();
        void setRowValues(int rowIndex, int ** values[]);
        void setColValues(int colIndex, int ** values[]);
        int rowCount, colCount, mtrix[][];
    protected:
    private:
};

#endif // MATRIX_H 

Here's the cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "matrix.h"
extern int rowCount, colCount, mtrix;
matrix::matrix(int numRows, int numCols)
{
    mtrix[numRows][numCols];
    rowCount=numRows;
    colCount=numCols;
}

void matrix::setRowValues(int rowIndex, int ** values[])
{
    for(int i = 0; i<colCount-1;i++)
        mtrix[rowIndex][i]=**values[i];

}

void setColValues(int colIndex, int ** values[])
{


}

matrix::~matrix()
{
    //dtor
}

UPDATE
btw, mtrix[rowIndex][i]=**values[i]; throws the error error: invalide types 'int[int]' for array subscript
Last edited on
The memory area is not allocated for the array.
"mtrix[][]" is not allowed. Only one dimension can be left variable.

If you want to decide both at runtime, you'll have to declare dynamically:
1
2
3
4
5
6
7
8
int **mtrix; // Declaration of a pointer to a pointer to an int
// Random code in between
mtrix = new int*[numRows]; 
for (int i = 0; i < numRows; ++i) {
  matrix[i] = new int[numCols]; 
}
// ...
matrix[i][j] = ...;

Interpretation
mtrix is a pointer to/an array of pointers to/arrays of ints.
mtrix[i] is a pointer to/an array of ints.
mtrix[i][j] is an int
Last edited on
wow, that's not very convenient, but it works. Thanks!
Topic archived. No new replies allowed.