Yeo Meng Tat
Yeo Meng Tat

Reputation: 25

Deleting an element in a vector by value

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?

When I compile, a error message is displayed: invalid conversion of 'int' to '*Flight*' at 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); 
}

Upvotes: 0

Views: 163

Answers (3)

Valentin Trinqu&#233;
Valentin Trinqu&#233;

Reputation: 1072

Since the code here isn't fully functional, it's hard to give you good advice.

First, your error happens because you call (what seems to be) the member function, void Agent::delete(Flight *obj), with a variable of type int instead of type Flight. The compiler is not able to interpret your Flight object as an int, so it throws an error.

Secondly, you want to know how to retrieve attributes from an object. I will advise you to have a look to accessors and mutators.

If you want to retrieve information hold in your Flight object, you should expose member functions allowing that.

// in your header file
class Flight
{
private:
    int flight_number;

public:
    // retrieve flight number value
    int get_flight_number(void) const;
    // allow to set the flight number value
    void set_flight_number(int new_flight_number);
    // some other member functions
}

// in your source file
int Flight::get_flight_number(void) const
{
   return this->flight_number;
}

void Flight::set_flight_number(int new_flight_number)
{
    // let's do some verification (do whatever you want)
    if (new_flight_number > 0)
    {
         this->flight_number = new_flight_number;
    }
}

This way you will be able to set and access your flight_number by writing, for example :

void test_function(Flight *f)
{
    if (f->get_flight_number() == 42)
    {
        // do some stuff
    }
}

int main()
{
    Flight *my_f = new Flight();
    my_f->set_flight_number = 4242;
    my_test_function(my_f);
}

Now, you have enough information to get going.

NOTES :

You heavily use pointers. Modern C++ strongly tends to not! Try to use references or move operation. You can consult this pages for info:

It's a bit hardcore for beginner though. The web is full of great article. about it

Upvotes: 1

Mohamad Elghawi
Mohamad Elghawi

Reputation: 2124

You original error is in your main method. You need to change it so that instead of passing the flight number to your delete method, you create an instance of your Flight class.

int main() { // you are also missing parenthesis
      Agent agent1;
      int flightNumber;
      cout<<"Enter the number of the flight: "<<flush; // I don't know what flush is but maybe you meant std::endl
      cin>>flightNumber;
      Flight flight(flightNumber);
      agent1.delete(&flight); // delete takes a Flight* not an int
}

This requires that your Flight class have an appropriate constructor.

class Flight
{
public:
    Flight(int flightNumber)
        : flightNumber_(flightNumber)
    {}

private:
    int flightNumber_;
};

Then in your delete method you search your vector for the Flight instance that has the same flightNumber_ as the Flight you want to remove from your vector. This will require your Flight class to have some way of returning it's flightNumber_ member variable.

This is definitely NOT the best way to do this and is far from being in accordance with modern C++ standards but it should get you going.

Upvotes: 0

NightFurry
NightFurry

Reputation: 358

You can add(if not present) a getter in Flight class

class Flight{
int FlightNumber;

public:

int getflightNumber(){ return flightNumber;}
};

and go as following:-

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;
    }
}

Upvotes: 1

Related Questions