Sign in to follow this  
lordimmortal2

Question About Vectors

Recommended Posts

lordimmortal2    130
In addition to my ignorance on templates, I am also pretty dumb on std::vector =P. I can't figure this bug out as I'm not quite sure why it's not working.

TileContainer.cpp:

[CODE]
void TileHandler::AddTile(int row, int column, int rowSpr, int colSpr)
{
Tile *tile = new Tile;
tile->rowPos = row;
tile->colPos = column;
tile->rowSpr = rowSpr;
tile->colSpr = colSpr;
TileContainer.push_back(tile);
}
[/CODE]

TileContainer is of type:
[CODE]typedef std::vector<Tile> TileContainer;[/CODE]

And the error is:
[img]http://i49.tinypic.com/2r27qs6.png[/img]

First thought was to mess around and make tile a double pointer, and things of this nature, but nothing worked. If anyone knows what's wrong with it, your answer is very appreciated.

Share this post


Link to post
Share on other sites
Ripiz    539
This looks like basic mistake.
TileContainer is a class, not variable, and you cannot store objects in a class, they're stored in an object.

I think you want to do this:
[CODE]
TileContainer myTileContainer; // probably should be member of TileHandler, not local variable
myTileContainer.push_back(tile);
[/CODE] Edited by Ripiz

Share this post


Link to post
Share on other sites
taby    1265
You've got a vector of Tile, but you're trying to push_back a Tile* (pointer to Tile).

Perhaps try changing your code to:

Tile tile;
tile.rowPos = row;
tile.colPos = column;
tile.rowSpr = rowSpr;
tile.colSpr = colSpr;
TileContainer.push_back(tile);

The whole point of vector is so that you don't have to use pointers and new/delete to store things in an array. All of the heap memory is automatically allocated/deallocated for you by the vector class. Perhaps I'm misreading what you're trying to do, but it doesn't seem like pointers are at all necessary here. Edited by taby

Share this post


Link to post
Share on other sites
NightCreature83    5002
[quote name='taby' timestamp='1336716423' post='4939218']
You've got a vector of Tile, but you're trying to push_back a Tile* (pointer to Tile).

Perhaps try changing your code to:

Tile tile;
tile.rowPos = row;
tile.colPos = column;
tile.rowSpr = rowSpr;
tile.colSpr = colSpr;
TileContainer.push_back(tile);

The whole point of vector is so that you don't have to use pointers and new/delete to store things in an array. All of the heap memory is automatically allocated/deallocated for you by the vector class. Perhaps I'm misreading what you're trying to do, but it doesn't seem like pointers are at all necessary here.
[/quote]

That is part of the problem yes and the cause of the compile error but as soon as the compile error is fixed another one should pop up informing you that you are using a class definition as a variable. To fix that one have a look at the Ripiz post above as that solution will fix your next problem.

For all STL containers, the object value you are trying to put into the container must match the type definition of the container, eg:
[code]
std::vector<int> m_intVector; //This can only hold variable of type int
std::vector<int*> m_intPointerVector; //This will only hold variables of type int*
[/code]

Be carefull with deep and shallow copies of data you push into the vector as the vector will actual make a copy of the object you pass in to [url="http://www.cplusplus.com/reference/stl/vector/push_back/"]push_back[/url] and store that in the vector.

Share this post


Link to post
Share on other sites
Kazuo5000    177
First thing:

[CODE]TileContainer[/CODE] is just a redefinition of [CODE]std::vector<Tile>[/CODE]. So you need to create a TileContainer: [CODE]TileContainer tileContainer;[/CODE]

Second thing:

you declare TileContainer to hold a Tile. You then declare a Tile pointer (Tile*) and then dynamically allocate memory for a Tile (new Tile()). Your vector container does not store Tile*, so you would need to need to either do:

[CODE]
Tile tile;
...
...
tileContainer.push_back(tile);
[/CODE]

or do:
[CODE]
tileContainer.push_back(*tile); // dereference the pointer to put a copy of the object into the vector
delete tile;
[/CODE]

Share this post


Link to post
Share on other sites
taby    1265
Clearly I missed something here, because everyone is talking about TileContainer being a class and not a variable? Where was the class defined? What the?

Share this post


Link to post
Share on other sites
Wan    1366
[quote name='taby' timestamp='1336769374' post='4939406']
Clearly I missed something here, because everyone is talking about TileContainer being a class and not a variable? Where was the class defined? What the?
[/quote]

The typedef enables the alternative name [i]TileContainer[/i] to be used instead of the more verbose [i]std::vector<Tile*>[/i]:

[CODE]typedef std::vector<Tile> TileContainer;[/CODE]

So either the OP intended to declare a variable but mistakenly added the typedef keyword, or he/she forgot to declare a (member) variable of type [i]TileContainer[/i] (or[i] std:vector<Tile*>[/i]) alltogether. Edited by Wan

Share this post


Link to post
Share on other sites
taby    1265
[quote name='Wan' timestamp='1336770249' post='4939410']
[quote name='taby' timestamp='1336769374' post='4939406']
Clearly I missed something here, because everyone is talking about TileContainer being a class and not a variable? Where was the class defined? What the?
[/quote]

[CODE]typedef ...[/CODE]
[/quote]

Oh man. I cannot believe I missed that, when it was right in front of my eyes. Thank you Wan, and sorry to the OP for not spotting that. Edited by taby

Share this post


Link to post
Share on other sites
lordimmortal2    130
[quote name='Ripiz' timestamp='1336715904' post='4939215']
This looks like basic mistake.
TileContainer is a class, not variable, and you cannot store objects in a class, they're stored in an object.

I think you want to do this:
[CODE]
TileContainer myTileContainer; // probably should be member of TileHandler, not local variable
myTileContainer.push_back(tile);
[/CODE]
[/quote]

I believe this fixed it (at least it compiled correctly when I changed it to something like this). Thank you for your help =).

[quote]You've got a vector of Tile, but you're trying to push_back a Tile* (pointer to Tile).

Perhaps try changing your code to:

Tile tile;
tile.rowPos = row;
tile.colPos = column;
tile.rowSpr = rowSpr;
tile.colSpr = colSpr;
TileContainer.push_back(tile);

The whole point of vector is so that you don't have to use pointers and new/delete to store things in an array. All of the heap memory is automatically allocated/deallocated for you by the vector class. Perhaps I'm misreading what you're trying to do, but it doesn't seem like pointers are at all necessary here.[/quote]

This is for adding a new tile for my engine to render. The way I'm at least attempting to do it right now is create a new struct of a tile, pushing that into a vector, and then iterating along the vector to render all tiles.

[quote]
[url="http://www.gamedev.net/index.php?app=forums&module=forums&section=findpost&pid=4939218"][img]http://public.gamedev.net//public/style_images/master/snapback.png[/img][/url]taby, on 11 May 2012 - 12:07 AM, said:

You've got a vector of Tile, but you're trying to push_back a Tile* (pointer to Tile).

Perhaps try changing your code to:

Tile tile;
tile.rowPos = row;
tile.colPos = column;
tile.rowSpr = rowSpr;
tile.colSpr = colSpr;
TileContainer.push_back(tile);

The whole point of vector is so that you don't have to use pointers and new/delete to store things in an array. All of the heap memory is automatically allocated/deallocated for you by the vector class. Perhaps I'm misreading what you're trying to do, but it doesn't seem like pointers are at all necessary here.

That is part of the problem yes and the cause of the compile error but as soon as the compile error is fixed another one should pop up informing you that you are using a class definition as a variable. To fix that one have a look at the Ripiz post above as that solution will fix your next problem.

For all STL containers, the object value you are trying to put into the container must match the type definition of the container, eg:
std::vector<int> m_intVector; //This can only hold variable of type int
std::vector<int*> m_intPointerVector; //This will only hold variables of type int*

Be carefull with deep and shallow copies of data you push into the vector as the vector will actual make a copy of the object you pass in to [url="http://www.cplusplus.com/reference/stl/vector/push_back/"]push_back[/url] and store that in the vector.
[/quote]

So can I assume that I'm doing something else wrong more so than trying to use a data type as an object of this data type? =P

[quote]First thing:

TileContainer
is just a redefinition of
std::vector<Tile>
. So you need to create a TileContainer:
TileContainer tileContainer;

Second thing:

you declare TileContainer to hold a Tile. You then declare a Tile pointer (Tile*) and then dynamically allocate memory for a Tile (new Tile()). Your vector container does not store Tile*, so you would need to need to either do:

Tile tile;
...
...
tileContainer.push_back(tile);

or do:
tileContainer.push_back(*tile); // dereference the pointer to put a copy of the object into the vector
delete tile;
[/quote]

Thanks for the catch, really would be bad to have that memory leak in there =P.

[quote]
[url="http://www.gamedev.net/index.php?app=forums&module=forums&section=findpost&pid=4939406"][img]http://public.gamedev.net//public/style_images/master/snapback.png[/img][/url]taby, on 11 May 2012 - 02:49 PM, said:

Clearly I missed something here, because everyone is talking about TileContainer being a class and not a variable? Where was the class defined? What the?

The typedef enables the alternative name [i]TileContainer[/i] to be used instead of the more verbose [i]std::vector<Tile*>[/i]:

typedef std::vector<Tile> TileContainer;

So either the OP intended to declare a variable but mistakenly added the typedef keyword, or he/she forgot to declare a (member) variable of type [i]TileContainer[/i] (or[i] std:vector<Tile*>[/i]) alltogether.
[/quote]

It was the latter. Stared at this for 20 minutes and nothing popped into my head about what was wrong...been so long since I've programmed last.

Thanks everyone for your help I really appreciate it =). I'll post again if I have anymore questions.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this