我目前有一个完美运行的main.cc和database.cc文件,但我的main.cc文件与要测试的文件不匹配。我试图转换它,但它一直在抛出错误,我没有时间打破我的整个代码并重新开始。
这是我的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();
}
这是我当前的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;
}
我为大量代码道歉,但我迫切需要帮助。谢谢!
答案 0 :(得分: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,first。您需要调整代码以便以这种方式调用您的方法,这意味着您需要通过在类实例之外的列表。
要掌握的重要一点是,您使用.
(或->
,如果您通过指针访问它)来访问您的功能成员;你无法直接将其称为函数。
您唯一的另一个选择是向您的database.cc添加包装函数,定义您尝试在新的main.cc中调用的函数。
答案 1 :(得分:0)
这是一个似乎要编译的新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();
}