实验室.cpp:112:14: 错误: 'std::vector<Invitee>'中没有名为'display'的成员


#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Date {
public:
Date() {
day = 12;
month = 31;
year = 2021;
}
Date(int x, int y, int z) {
day = x;
month = y;
year = z;
}
void GetDate() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
}
private:
int day;
int month;
int year;
};
class Time {
public:
Time() {
hour = 12;
minute = 30;
}
Time(int x, int y) {
hour = x;
minute = y;
}
void GetTime() { cout << "Time: " << hour << ":" << minute << "n"; }
private:
int hour;
int minute;
};
class Invitee {
string email;
public:
Invitee() { email = "jake_alvarado@csufresno.edu"; }
Invitee(string id) {
if (checkId(id) == true) {
email = id;
}
}
bool checkId(string &id) {
if (id.length() - 14 < 3) {
cout << "Invalid Email. Please Input New Email: n";
cin >> id;
checkId(id);
}
if (isdigit(id.at(0))) {
cout << "Invalid Email. Please Input New Email: n";
cin >> id;
checkId(id);
};
return true;
}
void display() { cout << "Email: " << email << endl; }
};
class Event {
string title;
string location;
Date *date;
Time *time;
vector<Invitee> list;
public:
Event(string t, string l, int day, int month, int year, int h, int m,
string e) {
title = t;
location = l;
date = new Date(day, month, year);
time = new Time(h, m);
list.push_back(e);
}
void display() {
cout << "Title: " << title << endl;
cout << "Location: " << location << endl;
date->GetDate();
time->GetTime();
list.display(); ///////////////////// This Line is Issue
}
};
int main() {
Event e1("Lunch", "Park", 9, 24, 2021, 12, 30, "jake_alvarado@csufresno.edu");
e1.display();
return 0;
}

我知道编译器在访问std和vector库和访问我的类Invitee之间感到困惑。但我不确定如何纠正这个问题。有人能帮我一下吗?

问题发生在类Event"我想在Class Invitee下的功能是"显示"。我也很确定我的显示功能不正确。

我想要它做的是显示vector<invitee> list内的字符串email。

listvector<Invitee>,只有std::vector类模板定义的成员函数,所以不能做list.display()。该成员函数不存在

你可以遍历存储在list中的Invitee,并在每个元素上调用display()成员函数:

for(Invitee& inv : list) inv.display();

一个不相关的建议:让所有的成员函数不能修改对象const:

void display() const {   // <- there
cout << "Email: " << email << endl;
}

这使得在const上下文中调用成员函数成为可能。

编译器报错,因为你正在调用vector.display(),它不存在。

相反,您希望遍历vector中的项并对它们调用disple

for (auto& member: list) {
member.display();
}

最新更新