如何从Flight中检索要与主输入(flightNumber)进行比较的对象?如何在main中声明属性类型?
编译时,显示错误消息:invalid conversion of 'int' to '*Flight*'
处的agent1.delete(flightNumber);
。
class Flight
{
int FlightNumber
};
class TravelAgent
{
vector <Flight *> flightList;
};
void Agent::delete(Flight *obj)
{
vector<Flight*>::iterator ptr;
for(ptr=flightList.begin();ptr!=flightList.end();ptr++)
{
if((*Ptr)==obj)
{
flightList.erase(ptr);
break;
}
}
if ((ptr) == flightList.end())
{
cout<<"Flight not found"<<endl;
}
}
int main
{
Agent agent1;
int flightNumber;
cout<<"Enter the number of the flight: "<<flush;
in>>flightNumber;
agent1.delete(flightNumber);
}
最佳答案
您可以在Flight类中添加(如果不存在)吸气剂
class Flight{
int FlightNumber;
public:
int getflightNumber(){ return flightNumber;}
};
并进行如下操作:
void Agent::delete(int flightNumber)
{
vector<Flight*>::iterator ptr;
for(ptr=flightList.begin();ptr!=flightList.end();ptr++)
{
if(((*Ptr)->getflightNumber() == flightNumber)
{
flightList.erase(ptr);
break;
}
}
if ((fPtr) == listFlight.end())
{
cout<<"Flight not found"<<endl;
}
}
关于c++ - 按值删除 vector 中的元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33776894/