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-friendoperator<<
, 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 standaloneoperator<<
.
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;