Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to C++ and my question is how can I retrieve an object from the Flight to be compared to the input (flightNumber) in the main? How do I declare the attributes type in the main? The error message displays "invalid conversion of 'int' to 'Flight' in the second last line.

C++
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); 
}
Posted
Updated 17-Nov-15 23:37pm
v2
Comments
Philippe Mori 18-Nov-15 12:50pm    
At one place, you have a vector of value and another one a vector of pointer... Either use one or the other.

And you should use a function object to find desired item using STL algorithms.

1 solution

Try:
C++
void Agent::delete(int flightNumber)
{
    vector<flight*>::iterator ptr;
    for(ptr=flightList.begin();ptr!=flightList.end();ptr++)
    {
        if( ptr->FlightNumber == flightNumber)
        {
           flightList.erase(ptr);
           return;
        }
    }
    if ((ptr) == flightList.end())
    {
        cout<<"Flight not found"<<endl;
    }
}


Please note, a std::vector is possibly NOT the best container for such a job (see http://www.cplusplus.com/reference/vector/vector/erase/[^] for details).
 
Share this answer
 
v2
Comments
Philippe Mori 18-Nov-15 12:48pm    
This code is incorrect if last flight is removed as it would display that the value was not found since end is now one item before...
CPallini 18-Nov-15 13:01pm    
Fixed now, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900