【发布时间】:2020-05-29 09:49:57
【问题描述】:
我制作了一类健身房通行证,它具有到期日期(int expire)、通行证价格(浮动价格)和持有人姓名(字符串数据)。它还有服务(char services),其中0是基本,1是免费淋浴,2是贵宾室。
这是类的定义:
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;
}
};
我还创建了一个类,它基本上是我的GymPass 类的向量。我正在尝试创建一个 void 函数来删除一张健身房通行证,但擦除会引发错误:
no instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=GymPass, _Alloc=std::allocator<GymPass>]" matches the argument list
就像一个优秀的程序员一样,我已经进行了数小时的谷歌搜索,但没有运气。这是体育课的定义
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 是删除功能,我得到错误。如何修复错误?
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
使用擦除时:不要循环增加索引!请参阅我对这个问题的回答:stackoverflow.com/questions/875103/…