# Vector of pointers isnt working for me. Something I did wrong?

## Recommended Posts

I have a bunch of objects that I put into an vector, which worked fine, then I put them as pointers to avoid double data replication and it would compile fine but went banana's when I ran it! If youre interested in helping me figure out whats going wrong, read on (Thank you) :-) The objects I'm storing in the vector are of the following struct:
struct Item
{
public:

double slicing;
double crushing;
double piercing;
double exploding;

// These will be obsolete with new character class!
/*float offset_stregnth;
float offset_offensive;
float offset_defensive;*/

//Offset defense parameters while item is equiped!
float offset_slicing;
float offset_crushing;
float offset_piercing;
float offset_exploding;

bool specialitem;
double requiredstregnth;
double weight;

std::string name;
std::string description;
ItemTypes type;

};As you may have noticed, it is for items in an RPG type game ;-)

The vector is created like this:

[code]std::vector<Item> AvailableItems;[/code]

and the objects are created and added to the vector like this:

//General Light Sword
Item Sword;
Sword.slicing = 10;
Sword.crushing = 2;
Sword.piercing = 5;
Sword.exploding = 0;
Sword.offset_slicing = 0;
Sword.offset_crushing = 0;
Sword.offset_piercing = 0;
Sword.offset_exploding = 0;
Sword.weight = 2;
Sword.requiredstregnth = 2;
Sword.specialitem = false;
Sword.description = "Just a basic Light Sword. Nothing fancy.";
Sword.name = "Light Sword";
Sword.type = WEAPON;
AvailableItems.push_back(Sword);


After all that I can display the "name" of the item by outputting it to the console like so: [code]std::cout << thisgame.AvailableItems[0].name << std::endl;[code]This all works fine, but we really want this as a pointer dont we?? well, I made what I thought to be the necessary adjustments which compiled but crashes when executed. The following is how I changed it ..... Structure stays the same ... Vector created like this:
std::vector<Item*> AvailableItems;
and object created the same way but added to the vector like this:
AvailableItems.push_back(&Sword);
which compiles without any warnings or errors, but gives errors and crashes when run. The following image is a screenshot of it running: Does anyone know how I can get this going like it should? I appreciate any imput on resulving this issue. Thank you :)

##### Share on other sites
You are creating your items like sword, etc. on the stack and what's probably happening is that they are going out of scope after you put the pointer to them in the vector, so the pointers being stored in the vector are pointing to garbage. If you want the items to persist, you should create them using new before you pass them to the vector. Just remember to delete them afterwards. (Or use smart pointers instead of normal pointers in your vector.)

##### Share on other sites
Doesnt make much sense to me ;-) I do know how to create an item using "new" though, so I'll see if I can give it a shot :)

Thank you very much.

##### Share on other sites
WooHoo !! :-) It works now!

Thank you very much. I think I inderstand the problem a little, so hopefully I can refer to this is I have a similar problem in the fututre.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628284
• Total Posts
2981829

• 10
• 10
• 10
• 11
• 17