简体   繁体   中英

C++ Linked List - Constructor and Operator Overloading

I've written an airline reservation system that runs off a linked list list<Passenger> flight list .

I think that the functions I've written should work, but I'm unable to test them as I can't get my passenger class right.

(database.h)

class Passenger
{
public:
    Passenger(string, string, string);
    ~Passenger();
    Passenger operator==(const Passenger&) const;
    Passenger operator<(const Passenger&) const;
    void read_from_file(list<Passenger>&, string);
    void insert(list<Passenger>&, string, string, string);
    void remove(list<Passenger>&, string, string, string);
    bool check_reservation(list<Passenger>&, string, string);
    void display_list(list<Passenger>&);
    void save_to_file(list<Passenger>&, string, string, string);

private:
    string fname;
    string lname;
    string destination;
};

(database.cc)

Passenger::Passenger(string first, string last, string dest)
{
     fname = first;
     lname = last;
     destination = dest;
}

Passenger::~Passenger()
{

}

Passenger Passenger::operator==(const Passenger&)
{

}

Passenger Passenger::operator<(const Passenger&)
{

}

The operators should be overloaded so that first names and last names are compared and so that the list is sorted lexicographically by last name.

Any point in the right direction would be so helpful.

Thanks!

The problem is the siganture of your operator overloads:

Passenger operator==(const Passenger&) const;
Passenger operator<(const Passenger&) const;

A comparison should return a bool , not a Passenger . So try with:

bool operator==(const Passenger&) const;
bool operator<(const Passenger&) const;

For example with the following implementation:

// and don't forget the trailng const to match the signature !
bool Passenger::operator==(const Passenger& p) const
{
    return fname==p.fname && lname==p.lname; 
}

bool Passenger::operator<(const Passenger& p) const
{
    return fname<p.fname || (fname==p.fname && lname<p.lname); 
}

And here the online demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM