Thread: Inserting pointer to an object in vector in a map

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Inserting pointer to an object in vector in a map

    Hi, I would greatly appreciate if you could help me fixing this.

    So, a the simulation runs, different body parts represented by classes declared in the Player Class (e.g. Head, Body, Legs and Weapons) take damage. When this happens, I am storing a pointer to the damaged part in a map belonging to the player class, with the key value representing the priority in the subsequent repair queue:

    In the player class:

    Code:
    map<int, shared_ptr<PartBase>, greater<int>> DamageLog;
    Example of storage:

    Code:
    auto HeadPtr = std::make_shared<Part>(Head);
    if (Head.Critical > 0) DamageLog.insert(make_pair(200, HeadPtr));
    With 200 being a priority repair value;

    Each part is derived from PartBase and each overrides the Repair() method. At the end parts get repaired by calling:

    Code:
    for (auto const& item : player.DamageLog) item.second->Repair();
    Now, the PROBLEM is that Weapons are stored in an array belonging to Player Class

    Code:
    vector<shared_ptr<Weapon>> Loadout;
    and I cannot insert them in the map using:

    Code:
    for (auto weapon : Loadout) {
    	if (weapon->Disabled) {
    		auto WeaponPtr = std::make_shared<Weapon>(weapon);
    		DamageLog.insert(make_pair(10, WeaponPtr));
    	}
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    So, you want multiple weapons to match on the same key? You can't do that with a map, I don't think. What you need is a multimap.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by GReaper View Post
    So, you want multiple weapons to match on the same key? You can't do that with a map, I don't think. What you need is a multimap.
    No, I want each weapon to have a unique key. But, you are right, each iteration of the loop moves the same pointer to a new weapon. But how can I insert a copy of pointers to disabled weapons?

    Also, the line in loop is ment to read

    Code:
    auto WeaponPtr = weapon;
    And I think this was the error.
    Last edited by serge; 09-22-2019 at 12:59 PM.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    115
    One more question in this vein: Does

    Code:
    DamageLog.clear();
    kill all pointers? I am rewriting the log each game turn instead of updating it (for security). So I am clearing at the beginning of the log function.

  5. #5
    Guest
    Guest
    Clearing the map will free all the smart pointers it contains, yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of object inside object not getting populated
    By progmateur in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2019, 05:36 AM
  2. Inserting New Element to a vector List
    By AC_Player in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2018, 09:08 PM
  3. Inserting struct data into a vector?
    By Glauber in forum C++ Programming
    Replies: 22
    Last Post: 05-27-2008, 09:58 AM
  4. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM
  5. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM

Tags for this Thread