Storing Obejcts in vectors

Hello

I'm terrible at C++ but I do my best to learn.
I'm trying to add objects of a class I created to a vector since they're going to vary greatly in numbers I don't want to use an Array.


But I'm having some problem with adding the objects (best case scenario, I can do it also using the constructor of that class.)

A bit of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//units
#ifndef UNIT_H
#define UNIT_H
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Brigade
{
      public:
             
             Brigade::Brigade(int a, int b){
                 m_number = a;
                 m_type = b;
             
// random functions 

      private:
              int m_number;
              int m_type;
              int m_endurance;
              int m_exp;
              };

class Army
{
      public:
             Army::Army(bool com = false, bool active = false){
                            bool m_active = active;
                            bool m_commander = com;
                            }
                            
                            int GetLocation(){return m_location;};
                            bool GetCommander(){return m_commander;};
                            int GetSupply(){return m_supply;};
                            string GetName(){return m_name;};
                                  
                            
                            void SetLocation(int);
                            void SetCommander(bool);
                            void SetSupply(int);
                            void SetName(string name){m_name = name;};
                            
                            void AddBrigade(){m_Brigades.push_back();};
                            
                            void RemoveBrigade(int nr){m_Brigades.pop_back(nr);};
                            void WipeBrigades(){m_Brigades.clear();};
                            
                            bool CheckEmpty(){m_Brigades.empty();};
      private:
              string m_name;
              int m_location;
              bool m_commander;
              int m_supply;
              vector<Brigade> m_Brigades;
              bool m_active;
};
      
#endif 



Other suggestions of how to improve my programming is welcome :).
So the problem is that I don't know how to add objects of type "Brigade" to the m_Brigade vector in Army.

PS if anyone have a suggestion for this that works there will be some changes in my code, the current version is in between array and vector :/.

1
2
3
void AddBrigade(const Brigade& newbrigade) { m_Brigades.push_back(newbrigade); }

Brigade RemoveBrigade() { return m_Brigades.pop_back(); }


You could call RemoveBrigade mutiple times in the user code to remove a number of Brigades or you could provide another member function RemoveBrigade(int nr); that can be used to remove more than one at a time.

In your constructors you should set all of the internal state of the object to some usable values. Don't leave m_endurance and m_exp undefined. (they could be set to 0 for example)

The public interface of a class is where you look to find out how to use the thing 6 months down the line when you have forgotten how it works. so
Brigade(int a,int b) could be written Brigade(int setnumber,int settype) or something similar

(you don't need the scope resolution operator :: for code in the class since you are already in the class)
Thanks for the tips

Why the first part? I'm not that good at this but I know that it's a reference to a Brigade object. Is it there to stop the vector to create a new copy of the object and consume more resource? So I would be glad if you explained that first part :)
void AddBrigade(const Brigade& newbrigade)

and the Wipe function I rewrote to
void WipeBrigades(){m_Brigades.erase(m_Brigades.begin(), m_Brigades.end())};
Last edited on
Topic archived. No new replies allowed.