How to declare arrays from existing class attributes?

Feb 24, 2016 at 4:00pm
It is showing error in array declearations a and b...Any SOLUTIONS??..Assume that i shall do work after array declaration.

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
46
47
48
#include<iostream>
using namespace std;
class matrix
{
int rows_1;
int rows_2;
int colums_1;
int colums_2;
	public:
		matrix(): rows_1(0),rows_2(0),colums_1(0),colums_2(0)	
		{	
		}
		void set()
		{int i,j;
			cout<<"Enter the rows of first matrix\n";
			cin>>rows_1;
			cout<<"Enter the rows of second matrix\n";
			cin>>rows_2;
			cout<<"Enter the columns of first matrix\n";
			cin>>colums_1;
			cout<<"Enter the columns of second matrix\n";
			cin>>colums_1;
			cout<<"Enter the elements of first matrix\n";
			for(i=0;i<rows_1;i++)
			{
				for(j=0;j<colums_1;j++)
				{
					cin>>a[i][j];
				}
			}
			cout<<"Enter the elements of second matrix";
			for(i=0;i<rows_2;i++)
			{
				for(j=0;j<colums_2;j++)
				{
					cin>>a[i][j];
				}
			}
		}
		int a[rows_1][colums_1];
		int b[rows_2][colums_2];
};
int main()
{
matrix mult;
mult.set();
mult.mult();
}
Last edited on Feb 24, 2016 at 4:38pm
Feb 24, 2016 at 4:54pm
If you want an array with size unknown at compile time, you have to create it using new.

You'd be better off just using vectors instead.
Feb 24, 2016 at 5:01pm
I would recommend to store only one matrix per class and create 2 matrix objects in main.
Topic archived. No new replies allowed.