0

I've made a class of gym pass that has expiration date (int expiration), price for the pass (float price) and name of its' holder (string data). It also has services (char services), where 0 is basic, 1 is with free showers and 2 is VIP room.

Here's the definition of class:

    class GymPass
    {
        int expiration; //the amount of days
        char services; //0 - basic, 1 - shower, 2 - VIP room
        float price = ((int)services - 47) * expiration;
        string data; //client's identifiable data
    public:
    
        GymPass()
        {
            cout << "this: " << this;
            expiration = 0;
            services = '0';
            price = 0;
            data = "Lorem ipsum";
        }
        Pass(int expiration, char services, string data)
        {
            this->expiration = expiration;
            this->services = services;
            this->data = data;
        }
    
        void Print()
        {
            cout << "Gym Pass: ";
            switch (services)
            {
            case '0':
            {
                cout << "Basic gym access, ";
            }
            case '1':
            {
                cout << "gym + shower room, ";
            }
            case '2':
            {
                cout << "Luxurious gym pass with VIP room, ";
            }
            default:
            {
                cout << "Error."; //this should never happen
            }
            }
            cout << "valid for: " << expiration << " days, price: " << price << " zl." << endl;
        }
        void NewPass()
        {
            cout << "Choose service type: \n0 - basic\n1 - showers included\n2 - vip room included";
            {
                char temp{};
                do
                {
                    cin >> temp;
                    if (!((int)temp > 47 & (int)temp < 51)) cout << endl << "Incorrect service type.";
                } while (!((int)temp > 47 & (int)temp < 51));
                this->services = temp;
            }
            cout << endl << "Input the expiration date of the Pass:";
            cin >> this->expiration;
            cout << endl << "Input your name: ";
            cin >> this->data;
        }
        friend bool operator==(const GymPass& lhs, const GymPass& rhs)
        {
            if (lhs.price != rhs.price) return false;
            if (!(lhs.data._Equal(rhs.data))) return false;
            if (lhs.services != rhs.services) return false;
            if (lhs.expiration != rhs.expiration) return false;
            return true;
        }
    };

I also made a class that's basically a vector of my GymPass classes. I'm trying to make a void function to delete one gym pass, but the erase throws error:

no instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=GymPass, _Alloc=std::allocator<GymPass>]" matches the argument list

Like a good programmer, I've done hours of googling, but to no luck. Here's definition of gym class

     class Gym
    {
        vector <GymPass> Clients;
    public:
        void add(GymPass pass)
        {
            Clients.push_back(pass);
        }
        void remove(GymPass pass)
        {
            for (int i = 0; i < Clients.size(); i++)
            {
                if (Clients[i] == pass) Clients.erase(i);
            }
        }
    };

void remove is the delete function, where I get the error. How do I fix the error?

KifoPL
  • 981
  • 7
  • 18
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 29 '20 at 10:48
  • 1
    When using erase : DON'T loop with increasing indices !! See my answer to this question : https://stackoverflow.com/questions/875103/how-do-i-erase-an-element-from-stdvector-by-index/63598736#63598736 – Pierre Baret Sep 06 '21 at 09:26

2 Answers2

1

std::vector::erase takes an iterator not an index as the argument.

Stali_Klienci.erase(Stali_Klienci.begin() + i);

is a reasonable hack. Stali_Klienci.begin() is the iterator to the first element, the + i increments the iterator to the ith element.

If you want to remove every element from a std::vector then clear does that in one call.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • That fixed it, thank you so much! Also, maybe it's a newbie question, but it looks really silly. Shouldn't vector convert index into an iterator on the fly? – KifoPL May 29 '20 at 09:53
0

You must read erase() doc first: erase() expects an iterator

Then something like this must do the job:

for (auto it = Stali_Klienci.begin(); it != Stali_Klienci.end(); ) 
{
   if (*it == karnet) {
        it = c.erase(it);
   } else {
        ++it;
   }
}
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70