0

I am getting an error when I am trying to erase an object from my vector

googled but I can't understand what's wrong. from what I gathered I am calling the erase function correctly but I have to provide a move assignment operator to my classes?

my function where i call the erase

void removegroup(const short &group){
switch (tracker[group].group){
    case 0: {
        guardvec.erase(guardvec.begin()+tracker[group].position); //this is the problem it compiles just fine if i comment out this line
        //guardvec.erase(remove(guardvec.begin(), guardvec.end(), tracker[group].position),guardvec.end()); //tried this and it doesnt work
    }

        break;
}
for (short counter=0;counter<tracker.size();counter++)
    if ((tracker[counter].group==tracker[group].group)&&(tracker[counter].position>tracker[group].position))
        tracker[counter].position=tracker[counter].position--;
}

my class

class guardhitdice {
    friend class guard;
    short const times = 2, dice = 8, plus = 2;
};

class guard : private guardhitdice {
private:
    short units, XP, morale, totalHP=0, dead=0, wounded=0;
    short* HP = new short[units];
public:
    void showstats(){
        std::cout << "STR=1, DEX=1, CON=1, INT=0, WIS=0, CHA=0, Perception=2, Passive Perception=12"<<std::endl
        <<"current xp: "<<XP<<"total HP across units: "<<totalHP<<"casualties: "<<dead<<"wounded: "<<wounded;
    }
    void initializeHP(){
        for (short counter=0; counter<units-1; counter++){
            HP[counter]=plus + D(times, dice);
            totalHP+=HP[counter];
        }
    }
   guard(const short &xp, short &Units, short &Morale){
       XP=xp;
       units=Units;
       morale=Morale;
      // short* HP = new short[units];
       initializeHP();
   }
    ~guard(){
        delete HP;
    }

};

here is my compiler error https://pastebin.com/KSniFkVD it should be able to remove the object at tracker[group].position. btw position is a short i also tried adding this to my classes but they don't work

guardhitdice& guardhitdice ::operator=(guardhitdice&&);
  guard& guard ::operator=(guard&&);

it works if I do it for normal vectors but not for vectors of objects

  • 1
    Make `short const times = 2, dice = 8, plus = 2;` `static`. They aren't changing from instance to instance and you can't assign to a `const` member. I also bet you could make a few other problems go away if `short* HP` was `vector HP` Right now you're violating the Rule of Three. – user4581301 Apr 20 '19 at 07:56
  • @user4581301 I will change I will change short* HP to use vectors instead – George Constantinides Apr 21 '19 at 08:40

1 Answers1

2

Your problem can be reduced down to

#include <vector>

class test{
    short const times = 2;
};

int main()
{
    std::vector<test> t;
    t.push_back(test{});
    t.erase(t.begin()); //<-- BAM!
}

Example: https://ideone.com/MlwUyI

vector does a lot of copy-and-assigning. Copying is cool, but test contains a const member. You can't change a const variable, so you can't assign it.

But since it's initialized with a literal and there's no constructor that'll allow the value to be initialized to something else, all instances of test will have the same value. That's a pretty good fit for static. static variables don't take part in assignment, so the default assignment operator is usable.

class test{
    static short const times = 2;
};

Example: https://ideone.com/npUi4B

user4581301
  • 33,082
  • 7
  • 33
  • 54