Hi guys!

I am trying to make an object oriented program for matrix addition, but I am stuck right at the beginning.

I would like to have an 2D array as a class variable and another two class variables would define the arrays x and y coordinates.

Something like this:

class matrix
              int n;
              int m;
              int array[n][m];

With this code I am getting error:"invalid use of non-static data member matrix::n/m"

I have also tried to make a dynamic 2D array but it didnt work as well.

Is there a way how to do this? Thanks.

The problem is that when the class is created, n and m do not have a value yet, so the compiler doesn't know how big you want your array. So you need to dynamicly create the array using new and delete. Here's how I'd do it:

class matrix
    int x;
    int y;
    int **arr;

matrix::matrix(int xin, int yin)
    x = xin;
    y = yin;
    arr = new int* [yin];
    for(int i = 0; i < yin; i++)
        *(arr+i)=new int[xin];

Don't forget to delete[] the arrays in your destructor!

With that said, I strongly recommend that you use vectors instead of arrays. It'll help you avoid memory-corruption and leakage.