c++ - 在 For 循环中使用数组时重载 Cout 运算符? C++

标签 c++ arrays for-loop cout

在我的随机分组排序器项目中,我收到一个错误,我不确定如何自行解决。 我针对此错误关注的代码块:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>

using namespace std;

class student
{
public:
    string nameFirst;
    string nameLast;
    string nameFull;
};

student typeName()
{
    student bar;
    cout << "Type in a student's first name: ";
    cin >> bar.nameFirst;
    cout << "Type in that student's last name: ";
    cin >> bar.nameLast;
    cout << "\n";
    bar.nameFull = bar.nameFirst + " " + bar.nameLast;
    return bar;
}

void options()
{

    cout << "Select what you want to do:\n";
    cout << "1) Exit application\n";
    cout << "2) Enter a Student\n";
    cout << "3) Display Students\n";
    cout << "4) Display Groups\n";
    cout << "5) Output groups as text file\n";
    cout << "\n";
}

int main()
{
    student allStudents[50]; // Having 50 students alone is ridiculous
    bool endProg = 0;
    int maxStudents;
    int studentsPerGroup;
    int optionSelect;
    int studentHeadCount = 0;
    cout << "GroupPicker 1.0\n";    
    cout << "How many students are in the class? (Note: You cannot have more than 50 in this program)\n";
    cin >> maxStudents;
    if (maxStudents > 50)
    {
        cout << "Too many students! Exiting program...\n";
        system("PAUSE");
        exit(1);
    }
    cout << "How many students per group?\n";
    cin >> studentsPerGroup;
    while (endProg == 0) {
        options();
        cin >> optionSelect;
        switch (optionSelect) {
            case 1:
                endProg = 1;
                break;
            case 2:
                allStudents[maxStudents] = typeName();
                studentHeadCount++;
                break;
            case 3:
                cout << "Current list of students:\n";
                for (int i = 0; i < studentHeadCount; i++)
                {
                    cout << allStudents[i] << endl; // error points to here
                }
                break;
            case 4: // Still coding this section
                if (studentHeadCount < studentsPerGroup)
                {
                    cout << "Invalid group parameters. Returning to main menu...\n";
                    break;
                }
                else
                {
                cout << "Here are the groups:\n";
                }
            case 5: // Still coding this section
                cout << "Saving groups to file...";
        }
    }
}

如果 3 是菜单输入,程序应该显示当前输入到 allStudents[i] 的学生列表,直到 istudentHeadCount。但是,我收到错误 C2679,因为它声称我使用的运算符无效。我是不是写错了什么?

最佳答案

您无法流式传输 student记录到cout除非你重载了 operator<<对于 student类型。为此,您需要类似以下内容:

// your existing student definition...
class student
{
public:
    string nameFirst;
    string nameLast;
    string nameFull;
};

// the new code you must add for operator<<
std::ostream& operator<<(std::ostream& os, const student& s)
{
     return os << s.nameFirst << ' ' << s.nameLast;
}

// continue with your program...
student typeName()
{
    ....

operator<<上面的函数是在 student 之外(和之后)定义的类,但你同样可以在 student 中定义它用friend前缀,如果你需要打印 private 会很方便数据成员,尽管可以选择:

  • 您可以改为提供公共(public) void print(std::ostream& os) const;成员并从非 friend 调用它operator<< , 或
  • 提供public从独立的 operator<< 中使用要打印的字段的“get”函数(在您不希望客户端代码使用的情况下有点难看) .

另外,如 Tim Z. 的回答和我的评论中所述,您的“选项 2”在几个方面被严重破坏,应该说:

case 2:
    if (studentHeadCount == maxStudents)
        std::cerr << "you can't enter more than " << maxStudents << " students\n";
    else
        allStudents[studentHeadCount++] = typeName();
    break;

关于c++ - 在 For 循环中使用数组时重载 Cout 运算符? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21744876/

相关文章:

java - 如何在 Mac 上的 JNI 构建中包含外部框架/库

javascript - JDK 1.8.0_92 Nashorn JS 引擎 indexOf 行为

javascript - ViewModel 中的 double 在 JavaScript 数组中四舍五入为整数

c++运行并发for循环,甚至可能吗?

c++ - "Sprintf"删除分配给调用中未调用的变量的值

c++ - nppiResizeSqrPixel_32f_C4R() 如何工作?

javascript - 展开 JS 对象并转换数组

java - 以某种方式打印出数组

Android For 循环不停止

javascript - 列出本地存储