setting a value in a vector.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

setting a value in a vector.

Post by ismetteren »

I am having a problem with the part of my game that loads a level from a text file. By lots of cout's i have tracked down the line that is causing the program to shut down(stop responding/answering - not sure what it is called in the eng. version of windows).

It looks like this:

Code: Select all

Tile t;
t.setX(k*32);
map.at(i).at(j) = t; //problem here!!
map is a vector of vectors of tiles. The only thing that has been done to the vector before this, is the creation in the class definition:

Code: Select all

vector<vector<Tile> > map;
I cant see why i should be able to do this, but if this is illeagel, what can i do instead?
Image ImageImage Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: setting a value in a vector.

Post by Ginto8 »

:shock: Why are you doing a vector of vectors? Are you doing an accumulated vector that includes every single level? 'cuz if it's just one level, all you need is a vector of tiles. Different vectors for different layers (if you're doing layers).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
BOMBERMAN
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Wed Jan 14, 2009 5:00 pm

Re: setting a value in a vector.

Post by BOMBERMAN »

Is "tile" a class or a structure or what?

If it's a class you should do like this: std::vector<tile*> Tiles;
And then do like this to create a new tile inside the vector: Tiles.push_back(new tile(constructor arguments...));
My english sucks!
User avatar
sparda
Chaos Rift Junior
Chaos Rift Junior
Posts: 291
Joined: Tue Sep 23, 2008 3:54 pm

Re: setting a value in a vector.

Post by sparda »

loads a level from a text file. By lots of cout's i have tracked down the line that is causing the program to shut down
You know, you guys should get in the habit of using a little debugging tool that will help you immensely. Every time you load a file, allocate data, or anything else that might cause a system crash (seg fault?), USE YOUR BRAINS!!! hahaha, okay, okay, I'm just kidding.

But seriously though, aside from normal exception handling (try, catch, or even a simple test if-statements), use the __LINE__ constant defined in <iostream> or <stdio.h> (for C).

That way, all you need to do is refer to the line where the program messed up, instead of going through a bunch of couts, or printfs.

Code: Select all

#include <stdio.h>
int main (int argc, char * const argv[]) {
	
	if( !load_this_shit("tiledata.dat") ) printf("Fucked up at line: %d", __LINE__);
    return 0;
	   
}
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: setting a value in a vector.

Post by MarauderIIC »

If your vectors haven't been allocated, "map.at(i).at(j) = t; //problem here!!" then i and j won't exist and this will throw an error. You need to push_back() or resize() (on both vectors).

So you might do this during level load:
innervector.push_back(every tile in a row)
outervector.push_back(innervector)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: setting a value in a vector.

Post by ismetteren »

Ginto8 wrote::shock: Why are you doing a vector of vectors? Are you doing an accumulated vector that includes every single level? 'cuz if it's just one level, all you need is a vector of tiles. Different vectors for different layers (if you're doing layers).
I dont know if it is a good way to do it in, but the position in my simulated 2D array, dicides where the tiles is located on the map. The x and y coordinate in my tile class just shows where on the tilesheet they are.

And thanks marauder. :D
Image ImageImage Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: setting a value in a vector.

Post by Ginto8 »

ismetteren wrote:
Ginto8 wrote::shock: Why are you doing a vector of vectors? Are you doing an accumulated vector that includes every single level? 'cuz if it's just one level, all you need is a vector of tiles. Different vectors for different layers (if you're doing layers).
I dont know if it is a good way to do it in, but the position in my simulated 2D array, dicides where the tiles is located on the map. The x and y coordinate in my tile class just shows where on the tilesheet they are.

And thanks marauder. :D
Here's an idea: make the x and y of the tile the x and y in TILES. When drawing them, just multiply the x and y by the width/height of the tiles. It'll make them easier to manage, methinks. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: setting a value in a vector.

Post by MarauderIIC »

If you have an upper bound on your map size, arrays are easier to deal with, not to mention faster (at least in comparison to using .at() instead of [][]) and with less overhead. Define a couple integer constants to use as upper bounds.
Last edited by MarauderIIC on Mon Feb 02, 2009 4:18 pm, edited 1 time in total.
Reason: faster...overhead
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply