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;
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:
std::vector<Item> AvailableItems;
and the objects are created and added to the vector like this:
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:
std::cout << thisgame.AvailableItems[0].name << std::endl;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 :)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend