我将如何使用 header 文件中预定义的友元方法?

How would I use a friend method that is predefined in a header file?

基本上,我的计算机科学老师让我使用朋友 std::ostream 方法进行输出。我已将其导入到 header 文件中,如下所示,但我不知道如何在student.cpp中使用它。添加 student::ostream 无效。我如何才能在 student.cpp

中使用 header 预定义方法

我的header文件

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

编辑: 当我前面没有 student:: 时,ostream 可以工作,但如果我在前面添加 student::,它会说它无法解析符号。我不确定没有 student:: 的那个是否正在使用我在 header 文件中定义的那个。

我将按以下方式进行:

  1. 在头文件和主文件中向函数 settingStudentName 添加正文和声明参数;
  2. 插入正确的依赖关系(#include);
  3. 添加一个main()驱动函数;
  4. 另外,使用正确的语法 (friend std::ostream& operator << (std::ostream&, const student &);) 缺少函数参数之间的逗号 , 分隔符。

MWE:

#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED

#include <iostream>
#include <string>
class student
{
public:
    student();
    std::string settingStudentName(const std::string&);
    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;
};

#endif // STUDENT_H_INCLUDED
#include "student.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

student::student()
{

}

string student::settingStudentName(const string& input)
{
    studentName = input;

    return input;
}

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;
}


int main()
{
    student Jack;
    Jack.settingStudentName("Jack");
    Jack.disiplineIssue();

    cout << Jack << endl;

    return 0;
}

这是输出:

Jack has a discipline issue

这是一个编译版本,你可以玩https://wandbox.org/permlink/mF49xQxkXs3M7n0M