• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lordimmortal2

Question About Vectors

8 posts in this topic

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

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[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.
1

Share this post


Link to post
Share on other sites
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]
1

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[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
1

Share this post


Link to post
Share on other sites
[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
0

Share this post


Link to post
Share on other sites
[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.
0

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  
Followers 0