Iterating a multidimensional std::vector using array notation?



 DEVELOP > c-Plus-Plus > Iterating a multidimensional std::vector using array notation?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "DevNull"
Date: 04 Jan 2008 11:25:18 AM
Object: Iterating a multidimensional std::vector using array notation?
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!
.

User: "Victor Bazarov"

Title: Re: Iterating a multidimensional std::vector using array notation? 04 Jan 2008 11:35:23 AM
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
.
User: "DevNull"

Title: Re: Iterating a multidimensional std::vector using array notation? 04 Jan 2008 11:38:34 AM
On Jan 4, 10:35 am, "Victor Bazarov" <v.Abaza...@comAcast.net> 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];
.
User: "DevNull"

Title: Re: Iterating a multidimensional std::vector using array notation? 04 Jan 2008 11:55:27 AM
On Jan 4, 10:38 am, DevNull <smor...@gmail.com> wrote:

On Jan 4, 10:35 am, "Victor Bazarov" <v.Abaza...@comAcast.net> 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!
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER