我目前有一个main.cc和一个database.cc文件,它们工作得很好,但我的main.cc文件与要测试的文件不匹配。我试过转换它,但它不断抛出错误,我没有时间破坏我的整个代码并重新开始。
这是我的数据库.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();
}
这是我当前的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;
}
这是我的程序将要测试的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;
}
我对大量的代码深表歉意,但我迫切需要帮助。非常感谢。
您的函数无法工作,因为您从未将它们定义为通用函数;他们是类函数成员。
你需要遍历你的列表并调用类成员——我不会重写你所有的代码,但这里有一个例子:
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 ...
}
注意,我调用MyMethod是作为一个类成员,而不是作为一个函数(iter,首先。你需要调整你的代码来调用你的方法,这意味着你需要在类实例之外查看你的列表。
需要掌握的重要一点是,您使用.
(或者->
,如果您通过指针访问它)来访问您的函数成员;不能直接将其作为函数调用。
您唯一的其他选择是将包装器函数添加到您的database.cc中,定义您试图在新的main.cc中调用的函数。
这是一个似乎要编译的新Database.cc。
测试main.cc似乎调用了自由函数,所以我在每个函数存根中都创建了新对象。
我在列表的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();
}