Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 5125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  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:

std::vector<Item> AvailableItems;

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;

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:
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 this post

Link to post
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 this post

Link to post
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 this post

Link to post
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!