2

I have numerous functions written to implement on a linked list of objects. All I have left to write is the display_list() and the save_to_file() functions, which are proving to be quite difficult for me. I currently have the code for how it is done with strings instead of Passenger objects, but none of my conversions work. Below are only the relevant parts of the files.

main.cc

case 5: 
{
    display_list();
    break;
}

case 6: 
{
    save_to_file("ticket_reservations.dat");
    break;
}

database.h

include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<fstream>
#ifndef passenger_h
#define passenger_h
 using std::string;
 using std::cin;
 using std::cout;
 using std::list;
 using std::endl;

class Passenger {
public:
    Passenger() {}
    Passenger(string, string, string);
    bool operator==(const Passenger&) const;
    bool operator<(const Passenger&) const;
private:
    string fname, lname, destination;

};

class Flightlist {
public:
    int menu();
    void read_from_file(string);
    void insert(Passenger p);
    void remove(Passenger p);
    bool check_reservation(Passenger p);
    void display_list();
    void save_to_file(string)
private:
    list<Passenger> flist;
};

#endif

database.cc

bool Flightlist::check_reservation(Passenger p)   //example of working function
{
    list<Passenger>::iterator i1, i2;
    i1 = flist.begin();
    i2 = flist.end();
    return flist.end() != find(flist.begin(), flist.end(), p);
}

void display_list()
{
    flist.sort();
    list<Passenger>::iterator i1, i2;
    i1 = flist.begin();
    i2 = flist.end();
    for ( ; i1 != i2; ++i1) {
        cout << *i1 << endl;
    }
}

void save_to_file(string filename)
{
    flist.sort();
    list<Passenger>::iterator i1, i2;
    i1 = flist.begin();
    i2 = flist.end();
    ofstream output(filename.c_str());
    for ( ; i1 != i2; ++i1) {
        output << *i1 << " ";
    }
    output.close();
}

The sort should take from the overloaded operators (== and <) so that they are sorted lexicographically on the last name.

Any help would be very appreciated!

Brittany
  • 119
  • 1
  • 10
  • 2
    From what I see, you seem to be missing the overloaded `operator <<` to perform stream-insertion. Is that fundamentally what you're having problems with? For operator overload questions, I refer you to [this question and its answers](https://stackoverflow.com/questions/4421706/operator-overloading), which you may find *very* helpful. Specifically, look for "Bitshift Operators (used for Stream I/O)" in the stellar selected answer. – WhozCraig Apr 03 '16 at 08:04

1 Answers1

0
bool Flightlist::check_reservation(Passenger p);

The argument of check_reservation function should be passed by reference, or the assgin function of the Passenger class should be implemented, which is absent in it.

bool Flightlist::check_reservation(Passenger & p);

or:

class Passenger {
public:
    Passenger() {}
    Passenger(string, string, string);
    Passenger(const Passenger&);
    void operator=(const Passenger&) const;
    bool operator==(const Passenger&) const;

    bool operator<(const Passenger&) const;
private:
    string fname, lname, destination;

};
lulyon
  • 6,707
  • 7
  • 32
  • 49
  • the check_reservation function is actually the only one that works there? – Brittany Apr 03 '16 at 07:52
  • @Brittany Otherwise, just implement the object assignment function for Pasenger – lulyon Apr 03 '16 at 07:53
  • I don't get what you mean? For the display_list and save_to_file classes? – Brittany Apr 03 '16 at 07:57
  • @Brittany add assignment function and assignment constructor to the Passenger class. – lulyon Apr 03 '16 at 07:58
  • @Brittany answer Updated – lulyon Apr 03 '16 at 07:58
  • 2
    @lulyon `check_reservation` doesn't modify the parameter, and the search will invoke `operator ==`. There is no reason apart from efficiency to make that param a reference (and it would be `const` ideally at that), nor is there a need to overload the default assignment operator which will deep-copy this class just fine as-is. (and *neither* of those will address the OP's problem anyway). Finally, on what planet is an (unnecessary) overloaded *assignment* operator `const` ? – WhozCraig Apr 03 '16 at 08:01
  • @WhozCraig thanks for that, I was very confused as to why the only working function was the one being commented on :/ – Brittany Apr 03 '16 at 08:31
  • 1
    @Brittany I urge you to check the link I provided up in general-comment. It covers a wide variety of topics about operator overloading, including what I think you seem to need: overloading stream-insertion, `std::ostream& operator << (std::ostream&, const Passenger&)` – WhozCraig Apr 03 '16 at 08:32
  • @WhozCraig yes the "<< " should be overloaded. But the OP's problem is with no speicific details, just guess. – lulyon Apr 03 '16 at 08:40