How to make global 2D array using vectors
-
I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"
#include
using namespace std;
extern int row;
extern int col;extern vector> _muPop(row, vector(col));
class ESSolver
{
public:
ESSolver();
void Init();
void Disp();private: vector< vector > \*muPop;
};
"ESSolver.cpp"
#include
#include
#include "../include/ESSolver.h"using namespace std;
ESSolver::ESSolver()
{
muPop = &_muPop;return;
}
void ESSolver::Init()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
(*muPop)[i][j] = i+1;
}
}
}void ESSolver::Disp()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << (*muPop)[i][j] << " ";
}
cout << endl;
}
}"main.cpp"
#include "../include/ESSolver.h"
#include #include using namespace std;int row;
int col;vector> _muPop(row, vector(col));
int main(int argc, char *argv[]){
//./main #row #colrow = atoi(argv\[1\]); col = atoi(argv\[2\]); ESSolver es; es.Init();
}
-
I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"
#include
using namespace std;
extern int row;
extern int col;extern vector> _muPop(row, vector(col));
class ESSolver
{
public:
ESSolver();
void Init();
void Disp();private: vector< vector > \*muPop;
};
"ESSolver.cpp"
#include
#include
#include "../include/ESSolver.h"using namespace std;
ESSolver::ESSolver()
{
muPop = &_muPop;return;
}
void ESSolver::Init()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
(*muPop)[i][j] = i+1;
}
}
}void ESSolver::Disp()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << (*muPop)[i][j] << " ";
}
cout << endl;
}
}"main.cpp"
#include "../include/ESSolver.h"
#include #include using namespace std;int row;
int col;vector> _muPop(row, vector(col));
int main(int argc, char *argv[]){
//./main #row #colrow = atoi(argv\[1\]); col = atoi(argv\[2\]); ESSolver es; es.Init();
}
-
I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"
#include
using namespace std;
extern int row;
extern int col;extern vector> _muPop(row, vector(col));
class ESSolver
{
public:
ESSolver();
void Init();
void Disp();private: vector< vector > \*muPop;
};
"ESSolver.cpp"
#include
#include
#include "../include/ESSolver.h"using namespace std;
ESSolver::ESSolver()
{
muPop = &_muPop;return;
}
void ESSolver::Init()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
(*muPop)[i][j] = i+1;
}
}
}void ESSolver::Disp()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << (*muPop)[i][j] << " ";
}
cout << endl;
}
}"main.cpp"
#include "../include/ESSolver.h"
#include #include using namespace std;int row;
int col;vector> _muPop(row, vector(col));
int main(int argc, char *argv[]){
//./main #row #colrow = atoi(argv\[1\]); col = atoi(argv\[2\]); ESSolver es; es.Init();
}
Your concept is totally wrong and won't work at least here:
vector> _muPop(row, vector(col));
because
row
andcol
are not defined. Write a class that holds the 2D array. That class should have an initialisation function with the number of rows and columns as parameters that allocates the array and initialises the array items. Then there is even no need to have a global instance of that class. It can be a local member of yourmain
function. You might also check if the usage ofvector
is really necessary. If you don't needvector
specific operations using classic C style arrays or C++array
templates might be a better option. -
I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"
#include
using namespace std;
extern int row;
extern int col;extern vector> _muPop(row, vector(col));
class ESSolver
{
public:
ESSolver();
void Init();
void Disp();private: vector< vector > \*muPop;
};
"ESSolver.cpp"
#include
#include
#include "../include/ESSolver.h"using namespace std;
ESSolver::ESSolver()
{
muPop = &_muPop;return;
}
void ESSolver::Init()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
(*muPop)[i][j] = i+1;
}
}
}void ESSolver::Disp()
{
// Initializing object variable
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << (*muPop)[i][j] << " ";
}
cout << endl;
}
}"main.cpp"
#include "../include/ESSolver.h"
#include #include using namespace std;int row;
int col;vector> _muPop(row, vector(col));
int main(int argc, char *argv[]){
//./main #row #colrow = atoi(argv\[1\]); col = atoi(argv\[2\]); ESSolver es; es.Init();
}
As suggested, don't use globals. Try, for instance
#include #include #include #include using namespace std;
class Matrix
{
int R, C;
vector > m;
public:
Matrix(int r, int c):R(r),C(c)
{
for (int r = 0; r());
for (int c = 0; c= 3)
{
rows = stoi(argv[1]);
cols = stoi(argv[2]);
}Matrix m(rows,cols);
cout << m << endl;
} -
As suggested, don't use globals. Try, for instance
#include #include #include #include using namespace std;
class Matrix
{
int R, C;
vector > m;
public:
Matrix(int r, int c):R(r),C(c)
{
for (int r = 0; r());
for (int c = 0; c= 3)
{
rows = stoi(argv[1]);
cols = stoi(argv[2]);
}Matrix m(rows,cols);
cout << m << endl;
}