c++ - 显示私有(private)数组变量 C++

标签 c++

我得到一个错误:在'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;
}

最佳答案

这里有两个问题:

  1. moduleprivate 并且不能在 main 中访问。使模块 public 或使用 get 方法。

  2. 以下将不起作用:

if (exams.module[i] == "COS1512")

应该是:

if (exams[i].module == "COS1512") 

这是因为 exams 是类 ExamType 的数组,并且类 ExamType 中没有定义 module 的数组

同样在cout语句中,exams.dateexams.time应该改为exams[i].date exams[i].time

关于c++ - 显示私有(private)数组变量 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52366525/

相关文章:

c++ - 为什么这个简单的 std::thread 示例不起作用?

c++ - 如果用户选择是,则执行 while 循环以重新启动程序,但行为不正确

c++ - 避免调用默认、移动和复制构造函数

c++ - 如何在 C++ 中减少较小字符串中的较大字符串?可能是通过哈希?

c++ - 对理解 C++ 代码的流程执行有一点帮助

c++ - 如果可以,我可以从文件中检索结构吗?

c++ - 什么时候执行后增量 i++?

c++ - 处理器是否支持异常?

c++ - 打包的结构没有预期的大小

c++ - 这个条件如何成立