简体   繁体   English

C ++使程序兼容

[英]C++ Making Program Compatible

I currently have a main.cc and a database.cc file that work perfectly, but my main.cc file does not match the one that it is going to be tested with. 我目前有一个main.cc和一个database.cc文件,它们可以正常工作,但是我的main.cc文件与将要测试的文件不匹配。 I've tried to convert it but it keeps throwing errors and I don't have the time to break my whole code and start again. 我已经尝试将其转换,但是它始终会引发错误,而且我没有时间破坏我的整个代码并重新开始。

This is my database.cc file: 这是我的database.cc文件:

#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;
    void print(std::ostream& os);
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


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

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

void Passenger::print(std::ostream& os)
{
    os << fname << ' ' << lname << ' ' << destination << '\n';
}

int Flightlist::menu()
{
    int option;
    cout << endl;
    cout << "Enter one of the following options:" << endl;
    cout << "1. load reservations from file:" << endl;
    cout << "2. reserve a ticket" << endl;
    cout << "3. cancel a reservation" << endl;
    cout << "4. check reservation" << endl;
    cout << "5. display passenger list" << endl; 
    cout << "6. save passenger list" << endl;
    cout << "7. exit" << endl << endl;
    cin >> option;
    cin.get();
    return option;
}

void Flightlist::read_from_file(string filename)
{
    string fname, lname, destination;
    std::ifstream input(filename.c_str());
    while (input >> fname >> lname >> destination) 
    {                   
        flist.push_back(Passenger(fname, lname, destination));
    }
    input.close();
}

void Flightlist::insert(Passenger p)
{
    flist.push_back(p);
}

void Flightlist::remove(Passenger p)
{
    flist.remove(p);
}

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

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

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

This is my current main.cc file, which works perfectly with the above program: 这是我当前的main.cc文件,可与上述程序完美配合:

#include "database.cc"

    int main()
    {   
        Flightlist flight_list;
        string fname, lname, destination;

    while (true) 
    {
        switch (flight_list.menu())
        {
            case 1: 
                {
                    flight_list.read_from_file("ticket_reservations.dat");
                    break;
                }

            case 2: 
                {
                    cout << "first name of passenger:" << endl; 
                    cin >> fname;
                    cout << "last name of passenger" << endl;
                    cin >> lname;
                    cout << "destination of passenger" << endl;
                    cin >> destination;
                    flight_list.insert(Passenger(fname, lname, destination));
                    break;
                }

            case 3: 
                {
                    cout << "first name of passenger:" << endl; 
                    cin >> fname;
                    cout << "last name of passenger" << endl;
                    cin >> lname;
                    cout << "destination of passenger" << endl;
                    cin >> destination;
                    flight_list.remove(Passenger(fname, lname, destination));
                    break;
                }

            case 4: 
                {
                    cout << "first name of passenger:" << endl; 
                    cin >> fname;
                    cout << "last name of passenger" << endl;
                    cin >> lname;
                    cout << "destination of passenger" << endl;
                    cin >> destination;
                    if (flight_list.check_reservation(Passenger(fname, lname, destination)))
                        cout << "this passenger has a ticket reservation" << endl;
                    else
                        cout << "this passenger does not have a ticket reservation" << endl;
                    break;
                }

            case 5: 
                {
                    flight_list.display_list();
                    break;
                }

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


            case 7:
                return 0;
        }
    }

    return 0;
}

This is the main.cc file that my program will be tested with, and therefore what I need to convert my code to be compatible with: 这是将与我的程序一起测试的main.cc文件,因此,我需要转换代码以使其兼容:

#include "database.cc"

int main()
{
    list<Passenger> flight_list;
    string first_name, last_name, destination;

    while (true) 
    {
        switch (menu())
        {
            case 1: 
                {
                    read_from_file(flight_list, "ticket_reservations.dat");
                    break;
                }

            case 2: 
                {
                    cout << "name of passenger:" << endl; 
                    cin >> first_name >> last_name;
                    cout << "destination:" << endl;
                    cin.ignore();
                    getline(cin, destination);
                    insert(flight_list, first_name, last_name, destination);
                    break;
                }

            case 3: 
                {
                    cout << "name of passenger:" << endl;
                    cin >> first_name >> last_name;
                    remove(flight_list, first_name, last_name);
                    break;
                }

            case 4: 
                {
                    cout << "name of passenger:" << endl;
                    cin >> first_name >> last_name;
                    if (check_reservation(flight_list, first_name, last_name))
                        cout << "this passenger has a ticket reservation" << endl;
                    else
                        cout << "this passenger does not have a ticket reservation" << endl;
                    break;
                }

            case 5: 
                {
                    display_list(flight_list);
                    break;
                }

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

            case 7:
                return 0;
        }
    }

    return 0;
}

I strongly apologise for the large amount of code, but I desperately need help with this. 对于大量代码,我深表歉意,但在此方面,我迫切需要帮助。 Thank you! 谢谢!

Your functions are not working because you have never defined them as general functions; 您的函数无法正常工作,因为您从未将它们定义为通用函数; they are class function members. 他们是班级职能成员。

You need to iterate through your list and call the class member - I'm not going to rewrite all your code, but here's an illustration: 您需要遍历列表并调用类成员-我不会重写所有代码,但是下面是一个示例:

list<MyClass> MyList;
// ... your list is populated here ..
for(auto iter=MyList.begin(); iter!=MyList.end(); ++iter) {
    // ... decide what to do here ...
    *iter->MyMethod(argument1,argument2)
    // ... etc ...
}

Note I'm calling MyMethod as a class member, not as a function (iter,first. You'll need to adjust your code to call your methods this way too, meaning you need to go through your list outside of the class instance. 注意,我将MyMethod作为类成员而不是函数进行调用(首先,它是函数。您也需要调整代码以这种方式调用方法,这意味着您需要遍历类实例之外的列表。

The important bit to grasp is that you use . 要掌握的重要一点是您使用. (or -> if you're accessing it through a pointer) to access your function member; (或者->如果要通过指针访问它)来访问函数成员; you can't call it directly as a function. 您不能直接将其作为函数调用。

The only other option you have is to add wrapper functions to your database.cc, defining the functions you're trying to call in your new main.cc. 您唯一的其他选择是将包装函数添加到database.cc,从而定义要在新的main.cc中调用的函数。

Here is a new Database.cc that seems to compile. 这是一个似乎可以编译的新Database.cc。

It seems that the test main.cc calls free functions so I created new objects in every function stub. 似乎测试main.cc调用了自由函数,所以我在每个函数存根中创建了新对象。

And I added a constructor to Flightlist for list. 然后,我向Flightlist添加了一个构造函数以获取列表。

    // #include "stdafx.h" // uncomment for MS version
    #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;
        void print(std::ostream& os);
    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);

    // --- CHANGES START ----------------

        Flightlist() {};
        ~Flightlist() {};
        Flightlist(list<Passenger> flistInput) : flist(flistInput){};
    private:
        list<Passenger> flist;
    };

    int menu() {
        static Flightlist FL; 
        return FL.menu();
    }

    void read_from_file(list<Passenger> flist, string s) {
        Flightlist FL(flist);
        FL.read_from_file(s);
    }

    void insert(list<Passenger> flist, string first, string last, string dest) {
        Flightlist FL(flist); 
        Passenger p(first, last, dest);
        FL.insert(p); 
    }

    void remove(list<Passenger> flist, string first, string last) {
        Flightlist FL(flist); 
        Passenger p(first, last, NULL);
        FL.remove(p);
    }

    bool check_reservation(list<Passenger> flist, string first, string last) {
        Flightlist FL(flist); 
        Passenger p(first, last, NULL);
        return FL.check_reservation(p);
    }

    void display_list(list<Passenger> flist) { 
        Flightlist FL(flist); 
        FL.display_list(); 
    }

    void save_to_file(list<Passenger> flist, string s) { 
        Flightlist FL(flist); 
        FL.save_to_file(s); 
    }

    // --- CHANGES STOP ----------------

    #endif


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

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

    void Passenger::print(std::ostream& os)
    {
        os << fname << ' ' << lname << ' ' << destination << '\n';
    }

    int Flightlist::menu()
    {
        int option;
        cout << endl;
        cout << "Enter one of the following options:" << endl;
        cout << "1. load reservations from file:" << endl;
        cout << "2. reserve a ticket" << endl;
        cout << "3. cancel a reservation" << endl;
        cout << "4. check reservation" << endl;
        cout << "5. display passenger list" << endl;
        cout << "6. save passenger list" << endl;
        cout << "7. exit" << endl << endl;
        cin >> option;
        cin.get();
        return option;
    }

    void Flightlist::read_from_file(string filename)
    {
        string fname, lname, destination;
        std::ifstream input(filename.c_str());
        while (input >> fname >> lname >> destination)
        {
            flist.push_back(Passenger(fname, lname, destination));
        }
        input.close();
    }

    void Flightlist::insert(Passenger p)
    {
        flist.push_back(p);
    }

    void Flightlist::remove(Passenger p)
    {
        flist.remove(p);
    }

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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