multidimensional array within classes

I've been working on this all day. My actual code is more complicated, but for now I'm stumped with the bit of code below. Running this, I get a referencing error, which seems absurd given the simplicity of the code. I'm just calling a private element within a class! However, various bad things have happened both in this and my more elaborate code, including lines and lines of random memory addresses and pathnames to "[heap]" or "[stack]" next to them. At a few points I got infinite loops from finite for iterations, but that will come later. For now, why can't I access this element?

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
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

template<class T>
class Matrix
{
public:
	Matrix();
	~Matrix();
	void printMatrix();
private:
	T M[2][2];
};

template<class T> Matrix<T>::Matrix()
{
	M={{1,0},{0,1}};
}
template<class T> Matrix<T>::~Matrix()
{
	delete[] M[0];
	delete[] M[1];
}
template<class T> void Matrix<T>::printMatrix()
{
	cout<<M[0][0];
}

int main()
{
	Matrix<int> a;
	a.printMatrix();
}


Thank you for your help.
LINE 26
1
2
delete[] M[0];
delete[] M[1];

you can't delete cos array is not alocated dynamicaly (no NEW no DELETE)
enough is only ONE delete [] M
second delete will chrash your app.

use for loop to initialize array if used dynamicali and use for loop to print whole matrix not m [0] [0] only.

Thank you, sasanet, for your helpful reply.
In the actual program, my array is allocated dynamically--but sticking to the present case, while I have made your changes, my main problem is that printMatrix doesn't print the value of M[0][0]. (Of course, if I can get it to give me this value, I will use a for loop as you suggest--but this is a very simple test program and I will be content with it if it will only display one element of my matrix class).
To be brief, even with the updated code (below), I cannot reach even the M[0][0] element, but instead get the error "Segmentation fault."

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
27
28
29
30
31
32
33
34
35
36
37
#include <string>

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

template<class T>
class Matrix
{
public:
	Matrix();
	~Matrix();
	void printMatrix();
private:
	T M[2][2];
};

template<class T> Matrix<T>::Matrix()
{
	M={{1,0},{0,1}};
}
template<class T> Matrix<T>::~Matrix()
{
	delete[] M;
}
template<class T> void Matrix<T>::printMatrix()
{
	cout<<M[0][0];
}

int main()
{
	Matrix<int> a;
	a.printMatrix();
}
Last edited on
Amazingly enough, even when I try to access a nondimensional element, I get the same error!

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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

template<class T>
class Matrix
{
public:
	Matrix();
	~Matrix();
	void printMatrix();
private:
	T M[2][2];
	T f;
};

template<class T> Matrix<T>::Matrix()
{
	M={{1,0},{0,1}};
	f=5;
}
template<class T> Matrix<T>::~Matrix()
{
	delete[] M;
}
template<class T> void Matrix<T>::printMatrix()
{
	cout<<f;
}

int main()
{
	Matrix<int> a;
	a.printMatrix();
}


And get this--the following doesn't work either--I removed the template and it gives the same error!

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
27
28
29
30
31
32
33
34
35
36
#include <string>

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

class Matrix
{
public:
	Matrix();
	~Matrix();
	void printMatrix();
private:
	int M[2][2];
};

Matrix::Matrix()
{
	M[0][0]=1;
}
Matrix::~Matrix()
{
	delete[] M;
}
void Matrix::printMatrix()
{
	cout<<M[0][0];
}

int main()
{
	Matrix a;
	a.printMatrix();
}


However, if I just use a simple variable (not two-dimensional), it works--what's going on here?
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
27
28
29
30
31
32
33
34
35
36
#include <string>

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

template<class T>
class Matrix
{
public:
	Matrix();
	~Matrix();
	void printMatrix();
private:
	T M;
};

template<class T> Matrix<T>::Matrix()
{
	M=321;
}
template<class T> Matrix<T>::~Matrix()
{
}
template<class T> void Matrix<T>::printMatrix()
{
	cout<<M;
}

int main()
{
	Matrix<int> a;
	a.printMatrix();
}


I've checked and the following is not improper:
 
int M[3][3];

as far as initialization of matrices go.

So why can't I access elements of my template-class's private two-dimensional from a function of that class?
The answer is that I must declare the two dimensional array as
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

template<class T>
class Matrix
{
public:
	Matrix();
	~Matrix();
	void printMatrix();
private:
	T* M[3];
};

template<class T> Matrix<T>::Matrix()
{
	for(int i=0;i<3;i++)
        {
                   M[i]=new int[3];
        }
        for(int i=0;i<3;i++)
                  for(int j=0;j<3;j++)
                             M[i][j]=(i==j)?1:0;
}
template<class T> Matrix<T>::~Matrix()
{
        for(int i=0;i<3;i++)
                delete[] M[i];
}
template<class T> void Matrix<T>::printMatrix()
{
	for(int i=0;i<3;i++)
                    for(int j=0;j<3;j++)
                               cout<<M[i][j];//with better formatting :D
}

int main()
{
	Matrix<int> a;
	a.printMatrix();
}
Topic archived. No new replies allowed.