Thread: Assessing Elements of a Struct in a Vector of Struct

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

    Assessing Elements of a Struct in a Vector of Struct

    Hello all!

    Why can I read the name from a weapon stored in loadout, but cannot set its value (i.e. disable is always false). Thank you very much for your help!

    Code:
    struct Weapon
    {
    	bool disabled = false;
    	std::string name;
    	Weapon(std::string n) : name(n) {}
    };
    
    struct Person {
    	std::string name;
    	std::vector<Weapon>Loadout;
    	Person(std::string n) : name(n) {}
    };
    
    typedef std::mt19937 rng_type;
    rng_type rng;
    
    // pick random element from array
    int Pick(rng_type& r, int n)
    {
    	std::uniform_int_distribution<int> dist(0, n - 1);
    	return dist(r);
    }
    
    template <typename Defender>
    void DisableRandomWeapon(Defender &d)
    {
    	auto weapon = d.Loadout.at(Pick(rng, d.Loadout.size()));
    	std::cout << weapon.name << std::endl;
    	weapon.disabled = true;
    }
    
    int main() {
    	rng_type::result_type const seed = std::random_device{}();
    	rng.seed(seed);
    	
    	Person A ("Joe");
    	
    	Weapon Pistol("Pistol");
    	Weapon Rifle("Rifle");
    	A.Loadout.push_back(Pistol);
    	A.Loadout.push_back(Rifle);
    	
    	DisableRandomWeapon(A);
    	std::cout << (int)A.Loadout.at(0).disabled << " " << (int) A.Loadout.at(1).disabled << std::endl;
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    In your Disable function you are "disabling" a weapon that is local to that function, not the weapon that is contained in any of your classes.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by jimblumberg View Post
    In your Disable function you are "disabling" a weapon that is local to that function, not the weapon that is contained in any of your classes.
    OK, I got this, thank you! But what I am trying to accomplish is something like this, where I want to call the overridden functions of the array elements. Can you help me do this?

    Code:
    #include <iostream>
    #include <vector>
    
    using std::cout;
    using std::endl;
    
    class B
    {
    	public:
    	virtual void f();
    };
    
    void B::f() {
    	cout << "B::f" << endl;
    }
    
    class D1 : public B
    {
    	public:
    	void f() override;
    };
    
    void D1::f() {
    	cout << "D1::f" << endl;
    }
    
    class D2 : public B
    {
    	public:
    	void f() override;
    };
    
    void D2::f() {
    	cout << "D2::f" << endl;
    }
    
    class C {
    	public : std::vector<B>arr;
    };
    
    int main()
    {
    	B b;
    	D1 d1;
    	D2 d2;
    	C c;
    	
    	c.arr.push_back(d1);
    	c.arr.push_back(d2);
    	
    	b.f();
    	d1.f();
    	d2.f();
    	
    	c.arr.at(0).f(); // this should be D1::f, not B::f
    	
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Code:
    c.arr.at(0).f(); // this should be D1::f, not B::f
    Well since C has a vector<B> member it will print the "overload" for B, not D1, remember C doesn't inherit anything from B.

    Perhaps you meant something more like:

    Code:
    ...
    
    class C {
        public : std::vector<B*>arr;
    };
    
    int main()
    {
        B b;
        D1 d1;
        D2 d2;
        C c;
    
        c.arr.push_back(&d1);
        c.arr.push_back(&d2);
    
        b.f();
        d1.f();
        d2.f();
    
        c.arr[0]->f(); // this should be D1::f, not B::f
    
        return 0;
    }


    By the way your first code would probably do what you wanted if you changed the function to something like:

    Code:
    template <typename Defender>
    void DisableRandomWeapon(Defender &d)
    {
        d.Loadout.at(Pick(rng, d.Loadout.size())).disabled = true;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in struct initializer
    By sergioms in forum C Programming
    Replies: 5
    Last Post: 12-06-2011, 11:25 AM
  2. default values for Struct elements
    By udaraliyanage in forum C Programming
    Replies: 2
    Last Post: 04-09-2011, 06:17 AM
  3. Access a struct elements from...
    By Blackroot in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2006, 05:00 AM
  4. Accessing elements in a struct
    By supaben34 in forum C++ Programming
    Replies: 8
    Last Post: 10-03-2004, 09:55 AM
  5. Elements of an array in a struct?
    By Ion Blade in forum C++ Programming
    Replies: 3
    Last Post: 08-03-2002, 03:02 PM

Tags for this Thread