Essentially, my Computer Science teacher is making me use the friend std::ostream method for output.. I have imported it into the header file as one can see below, but I have no clue how to use it in the student.cpp. Adding student::ostream did not work. How would I be able to use the header predefined method in my student.cpp
My header file
#pragma once
#include <iostream>
class student
{
public:
student();
std::string settingStudentName;
bool disiplineIssue();
// provided again so you learn this valuable override method for printing class data.
friend std::ostream& operator << (std::ostream& const student &);
private:
std::string studentName;
bool hasDisciplineIssue;
};
Student.cpp
#include "student.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
student::student()
{
}
bool student::disiplineIssue()
{
// Max the random integer can go
int max = 100;
srand(time(0));
int random = rand()%max;
// bool variable for returning
bool True_or_False = false;
if (random <= 5)
{
True_or_False = true;
}
return True_or_False;
}
ostream& operator<< (ostream& output, const student& aStudent) {
output << aStudent.studentName << " ";
if (aStudent.hasDisciplineIssue) {
output << "has ";
}
else {
output << "doesn't have ";
}
output << "a discipline issue";
return output;
}
Edit:
When I do not have student:: in front, ostream works but if I add student:: in front, it says it can not resolve the symbol. I am not sure if the one without student:: is using the one I defined in the header file.
question from:
https://stackoverflow.com/questions/65848294/how-would-i-use-a-friend-method-that-is-predefined-in-a-header-file 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…