Iterating a multidimensional std::vector using array notation?

Discussion in 'C++' started by DevNull, Jan 4, 2008.

  1. DevNull

    DevNull Guest

    I have a program where we load a mapfile, comprised of a .csv with
    numbers which represent object types at a given position.
    Each position is in the map is called a Cell, a cell contains only a
    position struct with it's own x,y and an int called CellType which
    determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e.
    empty space.

    Given...

    <code>
    typedef std::vector<Cell* > CellList;
    typedef std::vector<CellList* > Grid;
    Grid RoomGrid;
    </code>

    Why does the following code not compile?

    <code>
    std::cout << "Showing Map: " << std::endl;
    unsigned int x = 0;
    while(x++ < RoomGrid.size())
    {
    unsigned int y = 0;
    while(y++ != RoomGrid[x]->size())
    {
    std::cout << RoomGrid[x][y]->getType() << ",";
    }
    std::cout << std::endl;
    }
    </code>

    Specifically I'm getting this error
    error: base operand of '->' has non-pointer type 'std::vector<Cell*,
    std::allocator<Cell*> >'

    If I change the line
    <code>
    std::cout << RoomGrid[x][y]->getType() << ",";
    </code>

    To read
    <code>
    std::cout << RoomGrid[x][y].getType() << ",";
    </code>

    I get an error of
    error: 'class std::vector<Cell*, std::allocator<Cell*> >' has no
    member named 'getType'

    That isn't true though because Cell does have a member called getType,
    and it looks to me like
    RoomGrid[x][y] is returning the CellList object rather than the cell.

    What am I missing here? How can this be fixed? I really need to make
    sure that array notation works properly on the RoomGrid object even if
    I need to redefine it.

    Thanks in advance!
     
    DevNull, Jan 4, 2008
    #1
    1. Advertising

  2. DevNull wrote:
    > I have a program where we load a mapfile, comprised of a .csv with
    > numbers which represent object types at a given position.
    > Each position is in the map is called a Cell, a cell contains only a
    > position struct with it's own x,y and an int called CellType which
    > determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e.
    > empty space.
    >
    > Given...
    >
    > <code>
    > typedef std::vector<Cell* > CellList;
    > typedef std::vector<CellList* > Grid;
    > Grid RoomGrid;
    > </code>
    >
    > Why does the following code not compile?
    >
    > <code>
    > std::cout << "Showing Map: " << std::endl;
    > unsigned int x = 0;
    > while(x++ < RoomGrid.size())
    > {
    > unsigned int y = 0;
    > while(y++ != RoomGrid[x]->size())
    > {
    > std::cout << RoomGrid[x][y]->getType() << ",";


    'RoomGrid[x]' is a _pointer to CellList_. Using indexing on it
    does not give you 'Cell', it gives you a _reference to CellList_.
    Apparently 'std::vector<Cell*>' does not have 'getType' member.

    You need to dereference 'RoomGrid[x]':

    ... (*RoomGrid[x])[y]->getType() ...

    > }
    > std::cout << std::endl;
    > }
    > </code>
    >
    > Specifically I'm getting this error
    > error: base operand of '->' has non-pointer type 'std::vector<Cell*,
    > std::allocator<Cell*> >'
    >
    > If I change the line
    > <code>
    > std::cout << RoomGrid[x][y]->getType() << ",";
    > </code>
    >
    > To read
    > <code>
    > std::cout << RoomGrid[x][y].getType() << ",";
    > </code>
    >
    > I get an error of
    > error: 'class std::vector<Cell*, std::allocator<Cell*> >' has no
    > member named 'getType'
    >
    > That isn't true though because Cell does have a member called getType,
    > and it looks to me like
    > RoomGrid[x][y] is returning the CellList object rather than the cell.
    >
    > What am I missing here? How can this be fixed? I really need to make
    > sure that array notation works properly on the RoomGrid object even if
    > I need to redefine it.


    See above

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 4, 2008
    #2
    1. Advertising

  3. DevNull

    DevNull Guest

    On Jan 4, 10:35 am, "Victor Bazarov" <> wrote:
    > DevNull wrote:
    > > I have a program where we load a mapfile, comprised of a .csv with
    > > numbers which represent object types at a given position.
    > > Each position is in the map is called a Cell, a cell contains only a
    > > position struct with it's own x,y and an int called CellType which
    > > determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e.
    > > empty space.

    >
    > > Given...

    >
    > > <code>
    > > typedef std::vector<Cell* > CellList;
    > > typedef std::vector<CellList* > Grid;
    > > Grid RoomGrid;
    > > </code>

    >
    > > Why does the following code not compile?

    >
    > > <code>
    > > std::cout << "Showing Map: " << std::endl;
    > > unsigned int x = 0;
    > > while(x++ < RoomGrid.size())
    > > {
    > > unsigned int y = 0;
    > > while(y++ != RoomGrid[x]->size())
    > > {
    > > std::cout << RoomGrid[x][y]->getType() << ",";

    >
    > 'RoomGrid[x]' is a _pointer to CellList_. Using indexing on it
    > does not give you 'Cell', it gives you a _reference to CellList_.
    > Apparently 'std::vector<Cell*>' does not have 'getType' member.
    >
    > You need to dereference 'RoomGrid[x]':
    >
    > ... (*RoomGrid[x])[y]->getType() ...
    >
    >
    >
    > > }
    > > std::cout << std::endl;
    > > }
    > > </code>

    >
    > > Specifically I'm getting this error
    > > error: base operand of '->' has non-pointer type 'std::vector<Cell*,
    > > std::allocator<Cell*> >'

    >
    > > If I change the line
    > > <code>
    > > std::cout << RoomGrid[x][y]->getType() << ",";
    > > </code>

    >
    > > To read
    > > <code>
    > > std::cout << RoomGrid[x][y].getType() << ",";
    > > </code>

    >
    > > I get an error of
    > > error: 'class std::vector<Cell*, std::allocator<Cell*> >' has no
    > > member named 'getType'

    >
    > > That isn't true though because Cell does have a member called getType,
    > > and it looks to me like
    > > RoomGrid[x][y] is returning the CellList object rather than the cell.

    >
    > > What am I missing here? How can this be fixed? I really need to make
    > > sure that array notation works properly on the RoomGrid object even if
    > > I need to redefine it.

    >
    > See above
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Yuck thats a bit ugly, is there a good way to redefine RoomGrid so I
    can use my already existing code which refferences cells as
    RoomGrid[x][y] ?
    For instance

    Cell* CurrentCell = RoomGrid[x][y];
     
    DevNull, Jan 4, 2008
    #3
  4. DevNull

    DevNull Guest

    On Jan 4, 10:38 am, DevNull <> wrote:
    > On Jan 4, 10:35 am, "Victor Bazarov" <> wrote:
    >
    >
    >
    > > DevNull wrote:
    > > > I have a program where we load a mapfile, comprised of a .csv with
    > > > numbers which represent object types at a given position.
    > > > Each position is in the map is called a Cell, a cell contains only a
    > > > position struct with it's own x,y and an int called CellType which
    > > > determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e.
    > > > empty space.

    >
    > > > Given...

    >
    > > > <code>
    > > > typedef std::vector<Cell* > CellList;
    > > > typedef std::vector<CellList* > Grid;
    > > > Grid RoomGrid;
    > > > </code>

    >
    > > > Why does the following code not compile?

    >
    > > > <code>
    > > > std::cout << "Showing Map: " << std::endl;
    > > > unsigned int x = 0;
    > > > while(x++ < RoomGrid.size())
    > > > {
    > > > unsigned int y = 0;
    > > > while(y++ != RoomGrid[x]->size())
    > > > {
    > > > std::cout << RoomGrid[x][y]->getType() << ",";

    >
    > > 'RoomGrid[x]' is a _pointer to CellList_. Using indexing on it
    > > does not give you 'Cell', it gives you a _reference to CellList_.
    > > Apparently 'std::vector<Cell*>' does not have 'getType' member.

    >
    > > You need to dereference 'RoomGrid[x]':

    >
    > > ... (*RoomGrid[x])[y]->getType() ...

    >
    > > > }
    > > > std::cout << std::endl;
    > > > }
    > > > </code>

    >
    > > > Specifically I'm getting this error
    > > > error: base operand of '->' has non-pointer type 'std::vector<Cell*,
    > > > std::allocator<Cell*> >'

    >
    > > > If I change the line
    > > > <code>
    > > > std::cout << RoomGrid[x][y]->getType() << ",";
    > > > </code>

    >
    > > > To read
    > > > <code>
    > > > std::cout << RoomGrid[x][y].getType() << ",";
    > > > </code>

    >
    > > > I get an error of
    > > > error: 'class std::vector<Cell*, std::allocator<Cell*> >' has no
    > > > member named 'getType'

    >
    > > > That isn't true though because Cell does have a member called getType,
    > > > and it looks to me like
    > > > RoomGrid[x][y] is returning the CellList object rather than the cell.

    >
    > > > What am I missing here? How can this be fixed? I really need to make
    > > > sure that array notation works properly on the RoomGrid object even if
    > > > I need to redefine it.

    >
    > > See above

    >
    > > V
    > > --
    > > Please remove capital 'A's when replying by e-mail
    > > I do not respond to top-posted replies, please don't ask

    >
    > Yuck thats a bit ugly, is there a good way to redefine RoomGrid so I
    > can use my already existing code which refferences cells as
    > RoomGrid[x][y] ?
    > For instance
    >
    > Cell* CurrentCell = RoomGrid[x][y];


    Ok I was able to fix this, the answer should have been obvious and I
    don't know how I missed it.
    //typedef std::vector<CellList* > Grid;
    typedef std::vector<std::vector<Cell*> > Grid;

    It works as expected now!
     
    DevNull, Jan 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    20
    Views:
    4,358
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    742
    Andrew Koenig
    Jan 15, 2006
  3. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,341
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  4. Rune Allnor
    Replies:
    4
    Views:
    965
    Rune Allnor
    Dec 11, 2008
  5. carl
    Replies:
    5
    Views:
    2,430
    James Kanze
    Nov 25, 2009
Loading...

Share This Page