Using 2D vector as a member of class matrix

Hi,

I am trying to declare a class matrix (containing 2D array) with following members:
int d1, d2; //dimensions
vector<vector<float>> p; // All values of matrix

I want to put all elements of matrix in double vector.
The code compiles fine. However, when I start setting the value, I get message that:

....
Expression: vector subscript out of range...

There is some error with function “set_value”.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Matrix.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"  //must for MS visual studio
#include <iostream>  //For function cout<<
#include <fstream>   //For I/O from file
#include <string>    //For string op
#include <cstdlib>   //??
#include <vector>

using namespace std;


class matrix
{
	int d1, d2; //dimensions
	vector<vector<float>> p; // All values of matrix
public:
	//We are not using the default constructor
	matrix(int, int); //Constructor will only create the space here LLL
	~matrix();
	void set_value(int, int, float); // first two args are element position specifiers.
};


matrix::matrix(int d11, int d22)
{
	d1=d11;
	d2=d22;
	vector<vector<float>> p(d2, vector<float>(d1,0)); 
}

matrix::~matrix()
{

	//I believe that I do not need to write a destructor as there is no pointer involved.
}
void matrix::set_value(int i, int j, float value)
{
	p[j][i] = value;
}

int main()
{
	int m, n; //dimensions
    int i,j;
	float value;
//Enter size m>>n
//Enter element one by one
//Use the for loop

	cout<<"Enter the dimensions m, n : \n";
	cin>>m>>n;

	cout<<"Value of m and n \n";
	cout<<m<<", "<<n;
	matrix A(m,n), B(m,n);

//Reading A from key
	cout<<"Enter the values of elements one by one: ROWWISE\n";
	for (i = 0; i< m; i++)
		for (j=0; j < n; j++)
		{
			//cout<<"\n Please Enter (";cout<<i; cout<<","; cout<<j ;cout<<") th element \n";
			cout<<"\n Please Enter ("<<i<<","<<j<<") th element \n";
			cin>>value;
			A.set_value(i,j,value);
		}


	cout<<"\n";
	system("pause");
	//cin.get(); 
	return 0;
}
This (within your constructor): vector<vector<float>> p(d2, vector<float>(d1,0)); actually defines a vector p which hides the p belonging to your matrix class!

Try this instead:

1
2
3
4
5
6
matrix::matrix(int d11, int d22)
{
         d1=d11;
         d2=d22;
         p.resize(d2, vector<float>(d1,0));
}
You may want to include exception handling as well.
Thanks shacktar and johnystarr.

shacktar,

your suggestion worksed. However, for some reason, the launching of console was taking 4-5 minutes. So, rather than using resizing, I have started using:
1
2
3
4
5
6
7
matrix::matrix(int d11, int d22)
{
	d1=d11;
	d2=d22;
	vector<vector<float>> p1(d2, vector<float>(d1,0));
	p = p1;
}


Anyway, shacktar, can you tell me how did you figure out the source of error?


johnnystarr,
Since I am new to C++, I only have limited idea about exception handling and I was under impression that it's used for memory allocation. Since vector is standard STL feature, I am assuming that it must be taking care of that.

Anyway, would you please show me an example in current context?

Regards,
Last edited on
Anyway, shacktar, can you tell me how did you figure out the source of error?

Well, any time the compiler sees something like (type) (identifier) ... ; it treats it as a new definition. Combine that with C++'s scoping rules and you've got yourself a lost variable.

For instance:

1
2
3
4
5
6
int x;

void myFunction()
{
   int x;//now "myFunction()" sees this x, not the one declared above
}


This is precisely what was happening with your case above, where the vector p in the matrix class constructor hid the p in the scope of the whole matrix class. Seeing this comes with experience, I suppose.

Also, note that vector p was already constructed here (using the vector class default constructor): vector<vector<float>> p; // All values of matrix , so it doesn't make sense to try and construct it again.
Topic archived. No new replies allowed.