c++, erasing something from a vector of instances?

Associate
Joined
10 Oct 2008
Posts
353
Location
Dundee
Hi, I have a vector that gets populated by instances. Though, it seems to be making the erase() functionality of vectors unusable :S
Here is what I am talking about:
Code:
class Ctest;
vector <Ctest> testObjects;
class Ctest{
  int id;
  int something1,something2;
  float whatever;
  public:
    Ctest(int setId){
      id = setId;
    }
    ~Ctest();
    someMethod();
    anotherMethod(){
      //... stuff
      //... more stuff
 
      testObjects.erase(id);
    }
};
 
int main(){
  testObjects.push_back(testObjects.size());
  testObjects.push_back(testObjects.size());
  testObjects.push_back(testObjects.size());
  testObjects[1].anotherMethod();
...

Now... when I try to compile (my actual code, not the example above) with gcc I get an error:
error: no matching function for call to 'std::vector<CenemyBullet,std::allocator<CenemyBullet>>::erase(u16&)'

What I think it is stupidly trying to do is find the erase member in the class I have defined... what is up with that? Is that normal or have I done something stupid (defining the vector globally between definitions for the class?).
If that is normal... doesn't that make vectors unusable for objects?

Download my full source. The code in question is within main.cpp, and yes I know there are a lot of other issues in the code ^_^ it's a WIP, ill get round to cleaning it. Unless of course those issues caused that problem.

Oh, I am compiling for the GBA, using devkit advanced. So you most likely won't be able to compile it straight off if you need to do so. If you want you can download the full thing (over 21MB).
 
The problem is that the vector erase function takes an Iterator as a parameter, not an Int.

You will need to find the correct class instance in vector then erase it.

Something like this...

Code:
anotherMethod(){
    //... stuff
    //... more stuff

    for(std::vector<Ctest>::iterator itr = testObjects.begin(); itr != testObjects.end(); ++itr)
    {
        if(itr->id == id)
        {
             testObjects.erase(itr);
             break;
        }
    }
}

or you could use std::find to get the iterator.

Erasing items from a vector is not particularly quick you might be better off using std::list.
 
Hey, are you on the CGT course up here? I just graduated from there and I'm now working at cohort studios :)

I didn't even know about classes in my first year! you must have done this before ;)
 
Hey, are you on the CGT course up here? I just graduated from there and I'm now working at cohort studios :)

I didn't even know about classes in my first year! you must have done this before ;)

Im doing the CGAD one, it's a mix of a bit less technical stuff and a bit more theory-style things... like mapping and such I guess
 
The problem is that the vector erase function takes an Iterator as a parameter, not an Int.

You will need to find the correct class instance in vector then erase it.

Something like this...

Code:
anotherMethod(){
    //... stuff
    //... more stuff

    for(std::vector<Ctest>::iterator itr = testObjects.begin(); itr != testObjects.end(); ++itr)
    {
        if(itr->id == id)
        {
             testObjects.erase(itr);
             break;
        }
    }
}

or you could use std::find to get the iterator.

Erasing items from a vector is not particularly quick you might be better off using std::list.

I found a great solution;
testObjects.erase(testObjects.begin() + id);

works very well
 
Back
Top Bottom