473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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!

Jan 4 '08 #1
3 2386
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
2
by: Dylan | last post by:
what is the best way of initializing a static std::vector data member with some values? (currently I just push_back some values in the constructor if the size == 0) thanks
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
3
by: mast2as | last post by:
sorry i am too sure how to write a more explicit subject but this code doesn't compile for the type string and I am not sure why (and I am not sure either how to describe the problem but by looking...
5
by: rottmanj | last post by:
I am trying to build a multidimensional vector array that allows for multiple child elements under each parent element, but I am having a heck of a time trying to figure out how to do it. Right...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.