Iterating a multidimensional std::vector using array notation?

D

DevNull

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!
 
V

Victor Bazarov

DevNull said:
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
 
D

DevNull

DevNull said:
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.

<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

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];
 
D

DevNull

DevNull said:
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

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top