Issue With Two Dimension Variable Array/vector

So, I have a program where size of the multidimension array is based on the value that comes from a file. I want to pass this array to the function so that it populates the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main
{
//Reads from the file and extracts the NumberOfNode from the file
  NumberOfNode = ...;
  myclassA classA;
  classA.method1(1,2,3);
};

Class myclassA
{
double   matrix[NumberOfNodes][NumberOfNodes]; // Here I am not sure how to pass NumberOfNodes to this class; Even then I could get error.
void Method1(int node1, int node2, int cost)
{
    matrix[node1][node2] = cost;
}

}


So,how do i resolve this issue. Thank you.
Last edited on
If reading from the file and getting the numberofnodes is dependent on your class to work, why not just stick that function in your class? Or just have it part of your constructor, since that would be part of initializing variables.
Even if I stick to the function or have it as a part of the constructor, looks like it does not allow me to declare a array with that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main
{
//Reads from the file and extracts the NumberOfNode from the file
  NumberOfNode = ...;
  myclassA classA;
class(NumberOfNode);
  classA.method1(1,2,3);
};

Class myclassA
{
int _numberOfNodes;
double   matrix[_numberOfNodes][_numberOfNodes]; // Here I am not sure how to pass NumberOfNodes to this class; Even then I could get error.

myclassA(int NumberOfNode)
{
  _numberOfNodes = NumberOfNode;
}
void Method1(int node1, int node2, int cost)
{
    matrix[node1][node2] = cost;
}

}
You're looking for the new keyword.
I am not sure if that worked, I still get the error at the class when I am declaring a matrix array -
double matrix[_numberOfNodes][_numberOfNodes];

Can I do this using vector <vector<int>> instead to make it as a multidimensional array? I just tried and got an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Class myclassA
{
int _numberOfNodes;
//double   matrix[_numberOfNodes][_numberOfNodes]; 

vector<vector <int>> matrix;

//myclassA(int NumberOfNode)
//{
  //_numberOfNodes = NumberOfNode;
//}

void Method1(int node1, int node2, int cost)
{
    matrix[node1][node2] = cost;  // Now I am getting error at this place
}

}
Topic archived. No new replies allowed.