【发布时间】:2020-08-08 02:50:41
【问题描述】:
class CTurist
{
private:
string name;
string country;
int age;
public:
CTurist()
{
name = "";
country = "";
age = 0;
}
CTurist(string n, string c, int a)
{
name = n;
country = c;
age = a;
}
CTurist(const CTurist &t)
{
name = t.name;
country = t.country;
age = t.age;
}
string get_name()
{
return name;
}
string get_country()
{
return country;
}
int get_age()
{
return age;
}
void set_name(string n)
{
name = n;
}
void set_country(string c)
{
country = c;
}
void set_age(int a)
{
age = a;
}
bool operator<(CTurist& t)
{
return this->age < t.age;
}
friend istream& operator >> (istream& istr, CTurist& t)
{
istr >> t.name >> t.country >> t.age;
return istr;
}
friend ostream& operator<<(ostream& ostr, const CTurist& t)
{
ostr << "\nName: " << t.name << ", country: " << t.country << ", age: " << t.age;
return ostr;
}
};
class CHotel
{
private:
string hotel_name;
int num_beds;
double aver_price;
vector<vector<CTurist>>v;
public:
CHotel()
{
hotel_name = "";
num_beds = 0;
aver_price = 0;
}
CHotel(string hn, int nb, double ap, vector<vector<CTurist>>&vec)
{
hotel_name = hn;
num_beds = nb;
aver_price = ap;
v = vec;
}
CHotel(const CHotel& h)
{
hotel_name = h.hotel_name;
num_beds = h.num_beds;
aver_price = h.aver_price;
v = h.v;
}
string get_hotel_name()
{
return hotel_name;
}
int get_num_beds()
{
return num_beds;
}
double get_aver_price()
{
return aver_price;
}
vector<vector<CTurist>> get_vector() {
return v;
}
void set_hotel_name(string hn)
{
hotel_name = hn;
}
void set_num_beds(int nb)
{
num_beds = nb;
}
void set_aver_price(double ap)
{
aver_price = ap;
}
bool operator<(CHotel& h)
{
return this->aver_price < h.aver_price;
}
friend istream& operator >> (istream& istr, CHotel& h)
{
int n;
CTurist tur;
istr >> h.hotel_name >> h.num_beds >> h.aver_price;
for (int i = 0; i <= 11; i++) {
istr >> n;
for (int j = 0; j < n; i++) {
istr >> tur;
v[i].push_back(tur);
}
}
return istr;
}
friend ostream& operator<<(ostream& ostr, const CHotel& h)
{
ostr << "Hotel name: " << h.hotel_name << ", number of beds: " << h.num_beds << ", average price: " << h.aver_price;
for(const auto& it: h.v)
{
for (const auto& itr : it)
ostr << itr;
}
return ostr;
}
void Output(ostream &ostr)
{
ostr << "\nHotel name: " << hotel_name << ", number of beds: " << num_beds << ", average price: " << aver_price;
for (const auto& it : v)
{
for (const auto& itr : it)
ostr << itr;
}
}
};
class CTurLine
{
string m_line;
vector<CHotel>m_hoteli;
public:
CTurLine(string l, vector<CHotel>& vec)
{
m_line = l;
m_hoteli = vec;
}
CTurLine(const string& filename)
{
ifstream ifile(filename);
if (ifile.is_open())
{
ifile >> m_line;
copy(istream_iterator<CHotel>(ifile), istream_iterator<CHotel>(), back_inserter(m_hoteli));
}
}
friend ostream& operator<<(ostream& ostr, const CTurLine& t)
{
ostr << "Turline: " << t.m_line;
for (const auto& it : t.m_hoteli)
{
ostr << it;
}
return ostr;
}
void Output(ostream &ostr)
{
ostr << m_line << endl;
copy(m_hoteli.begin(), m_hoteli.end(), ostream_iterator<CHotel>(ostr, " "));
}
};
int main()
{
try {
CTurLine tur("data.txt");
tur.Output(cout);
}
catch (exception &e) //catch(char *c) -AKO E SAMO THROW
{ //{
cout << e.what() << endl; //cout<<c<<endl;
}
}
所以这是我的学校项目。我在读取对象(向量的向量)时遇到问题,并且我的程序没有运行。问题出在 CHotel 类中的朋友 ostream 运算符内部。
我将上传 2 张图片。在第一张您将看到我的文本文件,在第二张您将看到我的错误列表。
我尝试使用 & 修复它,如错误列表中的建议所示,但它没有帮助。
有人能告诉我如何解决吗?
text file
Albena
玉兰 54 110
3
伊万 bg 50
玛丽亚 bg 40
pesho pl 10
3
瓦莱里英国 15
ivelina 英国 35
gosho pl 31
3
詹姆斯 美国 52
卡特琳 45 美国
41 号码头
3
raluca ro 60
金美国 20
威廉 美国 18
3
lenka cz 20
马丁 cz 26
阿德里安 pl 65
3
kalina ro 45
莫妮卡 sp 40
jared 美国 49
3
丹尼尔 bg 17
teq bg 23
丹妮拉 bg 30
3
米伦 bg 43
瓦伦丁 bg 43
安德烈 bg 21
3
阿德里安娜 美国 19
gergana 英国 21
vasilena bg 30
3
vanq bg 23
venci bg 30
graciela bg 21
3
todorka bg 46
stanislav bg 34
马丁娜 bg 65
3
康拉德美国 26
borko ro 45
朱莉娅英国 31
【问题讨论】:
-
在
friend istream& operator >> (istream& istr, CHotel& h),你有v[i].push_back(tur);,但应该是h.v[i].push_back(tur);…… PS:请不要发布错误图片,而是将错误复制到问题中 -
请提供文本作为文本,而不是图像。
-
@ChrisMM 好的,我现在将在下一篇文章中,但是当我添加 h.在向量之前这是我得到的消息:Debug Assertion Failed
-
@Samantha 那是因为向量
h.v的大小为零。因此,当您执行h.v[i]时,您会收到错误消息。 -
@john 那我应该如何修复它?