trouble declaring matrix arrays

hey guys. im working on a project an array, be able to access them, and manipulate them at any time. my issue is i cant get anything to declare right at this point. i have the functions slashed out until i could figure out what i was doing wrong. any help would be amazing.

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
49
50
51
52
53
 #include <iostream>
using namespace std;
enum {size = 10};

class Matrix
{
public:
	void Initialize(int i, int j);
	int GetRow;
	int GetCol;
	int Add;
	int Subtract;
	int Print;
	int Copy;
	void MakeEmpty();
	void StoreValue(int, int, int);
	double matrix(int ,int);
	//int matrix();
private:
	int n, row, col;
	int data[size][size];
};

//matrix(int i, int j)
//{
//	
//}

//void StoreValue(int i, int j, int value)
//{
//	double matrix;
//	cout << "enter value" << endl;
//	cin >> value;
//	matrix[i][j] = value;
//};

//void MakeEmpty(int i, int j, int data[int][int])
//{
//	for (i = 0; i < size; i++) {
//		for (j = 0; j < size; j++) {
//			data[i][j] = 0;
//			
//		}
//	}
//};


int main()
{
	return 0;
};

Are you intending these to be constructors for Matrix?

1
2
	double matrix(int ,int);
	//int matrix(); 


If yes, constructors don't have any return type (not even void). They should match the name of the class (e.g. Matrix, not matrix)


If you're creating a Matrix object, you wouldn't declare it to be of a different type like double // double matrix; it would be its own type, possibly sending parameters in to the constructor

// Matrix myMatrix (4,5);


If you're defining a class function outside the class //matrix(int i, int j) , add the scope Matrix:: so the compiler knows it belongs to the class.
Matrix::Matrix(int i, int j)


The Matrix object has a private member data that is the 2d array. So it would be accessed through myMatrix.data[i][j] = value;, not matrix[i][j] = value;


There's more on classes and constructors here.
http://www.cplusplus.com/doc/tutorial/classes/
ok, everything made sense but the myMatrix part im not understanding where that came from and why we use it. would that be the solution that im looking for to get, for example, MakeEmpy() to funciton correctly? because the code is right, it just the array there cant be recognized.
You're defining your own user type Matrix. So if I'm creating a Matrix object, I'm just giving it a name, in this case, myMatrix. You could call it something else. Like if you created an integer type variable, you'd make a declaration with the data type int then the name of the variable.

e.g.

1
2
int row; //declares a variable of type int named row 
Matrix myMatrix; //declares an object of class Matrix called myMatrix.   


The MakeEmpty is just supposed to assign 0 to all the elements of the 2d array (fixed size 10 x 10)? Something like this?

1
2
3
4
5
6
7
8
void Matrix::MakeEmpty()
{
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
			data[i][j] = 0;
	}
}
Last edited on
This doesn't declare the variable i that's used as the for loop counter.

for (i = 0; i < size; i++)

This does declare i as an int.

for (int i = 0; i < size; i++)
Last edited on
thank you so much. i see exactly where i was messing up now. just small stuff i left out. im coming off a 2 year programming hiatus so small stuff like that ive forgotten. thank you a lot
Topic archived. No new replies allowed.