Search code examples
c++vectorstructerase

Erase elements from vector struct


So I started learning vector and I wanted to erase a element from a struct vector, I have this as struct:

typedef struct Carro{
   int id, cc, cv;
   char marca[50], modelo[50];
}car;

typedef struct Condutor{
   vector<car> cars;
   int id;
   int totalC=0;
   char nome[50];
}driver;

and this to delete:

for(int i=0; i< (*ptr).size(); i++){
    if((*ptr)[i].id == id){
        (*ptr).erase((*ptr).begin +i);
        verif=true;
        break;
    }
    else{
        verif=false;
    }
}

but it doesn't seem to work as I get this error in the erase line while trying to run it:

invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator+'

How can I delete an element from vector?


Solution

  • Without knowing what ptr is, this is a bit a of a guess, but you probably want instead of:

      (*ptr).erase((*ptr).begin +i);
    

    this:

     ptr->erase( ptr->begin() +i);
    

    begin() is a function - your code tries to treat it as a function pointer.