Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Question About Vectors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 lordimmortal2   Members   -  Reputation: 129

Like
1Likes
Like

Posted 10 May 2012 - 11:49 PM

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:
Posted Image

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.

Sponsor:

#2 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 10 May 2012 - 11:58 PM

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

Edited by Ripiz, 10 May 2012 - 11:59 PM.


#3 taby   Members   -  Reputation: 336

Like
0Likes
Like

Posted 11 May 2012 - 12:07 AM

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, 11 May 2012 - 12:13 AM.


#4 NightCreature83   Crossbones+   -  Reputation: 3025

Like
1Likes
Like

Posted 11 May 2012 - 01:54 AM

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, Mad Max

#5 Kazuo5000   Members   -  Reputation: 170

Like
1Likes
Like

Posted 11 May 2012 - 02:16 AM

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;


#6 taby   Members   -  Reputation: 336

Like
0Likes
Like

Posted 11 May 2012 - 02:49 PM

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?

#7 Wan   Members   -  Reputation: 1366

Like
1Likes
Like

Posted 11 May 2012 - 03:04 PM

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.

Edited by Wan, 11 May 2012 - 03:06 PM.


#8 taby   Members   -  Reputation: 336

Like
0Likes
Like

Posted 11 May 2012 - 03:16 PM


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 ...


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, 11 May 2012 - 03:20 PM.


#9 lordimmortal2   Members   -  Reputation: 129

Like
0Likes
Like

Posted 11 May 2012 - 10:00 PM

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.


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.

Posted Imagetaby, 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.


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;


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

Posted Imagetaby, 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.


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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS