I have a small struct containing a bool, char & two ints. But when I try to run this code, I get into a problem.
struct Struct
{
bool check;
char display;
int x,y;
};
typedef vector<Struct> Array;
vector<Array> Matrix;
for (int i = 0; i < rows; i++)
{
vector<Struct> temp;
for (int j = 0; j < cols; j++)
{
temp[i].push_back(Matrix);
}
Matrix.push_back(temp);
}
I want to fill my 2D array so that I would later be able to write something in terms of:
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Matrix[i][j].display = '*';
}
}
However, as of now, I get the error: "temp has no member push_back()".
You are referring temp[i]
. As temp is not an array or a vector of vectors it can't be used like that. When you do temp[i]
you are getting the i'th
element of vector temp, which is a Struct
, which has not a push_back
method.
You can do something like this to initialize the Matrix
:
....
for (int i = 0; i < rows; i++)
{
vector<Struct> temp;
for (int j = 0; j < cols; j++)
{
Struct s;
s.check = true;
s.display = 'c';
s.x = i;
s.y = j;
temp.push_back(s);
}
Matrix.push_back(temp);
}
....