我得到一个错误:在'exams'中请求成员'module',它属于非类'ExamType [12] - 这是一个我不明白的工作分配问题。
我们将不胜感激。
代码
#include <iostream>
#include <string>
using namespace std;
class ExamType
{
public:
ExamType();
ExamType(string m, string v, int t, string d);
private:
string module;
string venue;
int time;
string date;
};
int main()
{
ExamType exams[12];
for (int i = 0; i < 12; i++)
if (exams.module[i] == "COS1512") cout << "COS1512 will be written on " << exams.date << " at " << exams.time;
return 0;
}
最佳答案
这里有两个问题:
module
是private
并且不能在main
中访问。使模块public
或使用 get 方法。以下将不起作用:
if (exams.module[i] == "COS1512")
应该是:
if (exams[i].module == "COS1512")
这是因为 exams
是类 ExamType
的数组,并且类 ExamType 中没有定义
。 module
的数组
同样在cout
语句中,exams.date
和exams.time
应该改为exams[i].date
和 exams[i].time
。
关于c++ - 显示私有(private)数组变量 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52366525/