By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

Iterating a multidimensional std::vector using array notation?

P: n/a
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!

Jan 4 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jan 4 '08 #2

P: n/a
On Jan 4, 10:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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];
Jan 4 '08 #3

P: n/a
On Jan 4, 10:38 am, DevNull <smor...@gmail.comwrote:
On Jan 4, 10:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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!
Jan 4 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.