Question About Vectors

Started by
7 comments, last by lordimmortal2 11 years, 11 months ago
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:


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);
}


TileContainer is of type:
typedef std::vector<Tile> TileContainer;

And the error is:
2r27qs6.png

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.
Prove me wrong so I can know what's right.
Advertisement
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:

TileContainer myTileContainer; // probably should be member of TileHandler, not local variable
myTileContainer.push_back(tile);
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.

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 push_back and store that in the vector.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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;
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?

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 TileContainer to be used instead of the more verbose std::vector<Tile*>:

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 TileContainer (or std:vector<Tile*>) alltogether.

[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?


typedef ...
[/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.

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:

TileContainer myTileContainer; // probably should be member of TileHandler, not local variable
myTileContainer.push_back(tile);



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

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.


snapback.pngtaby, 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 push_back 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

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.


snapback.pngtaby, 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 TileContainer to be used instead of the more verbose std::vector<Tile*>:

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 TileContainer (or std:vector<Tile*>) 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.
Prove me wrong so I can know what's right.

This topic is closed to new replies.

Advertisement