Inserting data into vector of structs

I am trying to insert member data into a vector of structs, but I receive the errors:

1
2
3
4
5
6
7
main.cpp: In function âint main()â:
main.cpp:28: error: âclass std::vector<std::vector<MazeNode, std::allocator<MazeNode> >,
 std::allocator<std::vector<MazeNode, std::allocator<MazeNode> > > >â has no member named ârowâ
main.cpp:29: error: âclass std::vector<std::vector<MazeNode, std::allocator<MazeNode> >, 
std::allocator<std::vector<MazeNode, std::allocator<MazeNode> > > >â has no member named âcolumnâ
main.cpp:30: error: âclass std::vector<std::vector<MazeNode, std::allocator<MazeNode> >, 
std::allocator<std::vector<MazeNode, std::allocator<MazeNode> > > >â has no member named âcellâ


And here is the code fragment giving me issues:

1
2
3
4
5
struct MazeNode {
   int row, column;
   char cell;
};


1
2
3
4
5
6
7
8
9
10
11
  vector<vector<MazeNode> > theMaze[r][c];

   for(int r_num = 0; r_num < r; r_num++)
        for(int c_num = 0; c_num < c; c_num++)
        {
                cin.get(cell_val);
                theMaze[r_num][c_num].row = r_num;
                theMaze[r_num][c_num].column = c_num;
                theMaze[r_num][c_num].cell = cell_val;
        }


So basically what I think is happening is that it will not allow me to access member data of a struct while it is in a vector, but I do not know a way around this. My question is, how do I insert struct member data into the vector?
Last edited on
Computergeek, I think you overread something:
 
vector<vector<MazeNode> > theMaze[r][c]


Your problem isn't with the accessing code, but there. You define an 1array of an 2array of a 3vector of a 4vector of MazeNodes here, while you only want a vector of a vector of MazeNodes. Just drop the [r][c] at the end of the declaration and it will work perfectly fine. And btw: you would do it like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int r_num = 0; r_num < r; r_num++)
{
        vector<MazeNode> tempVector;
        for(int c_num = 0; c_num < c; c_num++)
        {
                MazeNode temp;
                cin.get(cell_val);
                temp.row = r_num;
                temp.column = c_num;
                temp.cell = cell_val;
                tempVector.push_back(temp);
        }
        theMaze.push_back(tempVector);
}
Last edited on
I can't help thinking he's trying to do something else though. There's technically nothing wrong with having a multidimentional array of vectors except in the way that he is trying to implement it (and the typedef, I have no idea what he's trying to accomplish there).
Last edited on
Computergeek01, thanks, I learned something new there even though it didn't directly apply to my question.

hanst99,

I tried my version and it seg faulted, so I pasted yours in and it works. I was figuring out why it worked, and thats why it took me so long to respond lol. But I understand why it works now, and why I didn't need the [r][c], I guess I put it there because I am new to vectors and was in the mindset of using arrays. Thanks a lot.
Argh! I was wrong about the definition, Vectors allow you to predefine their size but you use '()' brackets not '[]' brackets to do it.
Yep, that's cause that's the vectors constructor arguments. And even then you couldn't have used it for a 2D vector (at least not in one line), so it doesn't really apply. If you wanted a constant size container, you might consider using a boost::array. (http://www.boost.org/doc/libs/1_46_1/doc/html/array.html )
Topic archived. No new replies allowed.