Domanda

In my random group sorter project, I am receiving an error that I am not sure how to resolve on my own. The code block I'm focusing on for this error:

#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...";
        }
    }
}

In the event that 3 is the menu input, the program should display the current list of students entered into allStudents[i] until the i matches the value of studentHeadCount. However, I am receiving an error C2679, because it claims that I'm using an invalid operator. Did I write something wrong?

È stato utile?

Soluzione

You can't stream a student record to cout unless you've overloaded operator<< for the student type. To do that, you need something like:

// 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()
{
    ....

The operator<< function above is defined outside (and after) the student class, but you could have equally defined it inside student with a friend prefix, which can be convenient if you need to print private data members, though alternatively:

  • you could instead provide a public void print(std::ostream& os) const; member and call it from a non-friend operator<<, or
  • provide public "get" functions for the fields you want to print (bit ugly in situations where you want to discourage use by client code) to be used from a standalone operator<<.

Separately, as mentioned in Tim Z.'s answer and my comments, your "option 2" is seriously broken in a couple ways, and should say:

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

Altri suggerimenti

You should define how to output a student instance by defining a function

ostream& operator<<(ostream& out, const student& stud)

I would comment, but my rep doesn't allow it yet...

Anyways, I don't see you checking for case 2 studentHeadCount if it ever passes 50. So if you loop over 50 times and you hit case 3. Index of out bounds.

Also:

Can you add your student class for us to see?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top