【问题标题】:How would I use a friend method that is predefined in a header file?我将如何使用在头文件中预定义的朋友方法?
【发布时间】:2021-01-22 15:46:33
【问题描述】:

基本上,我的计算机科学老师让我使用朋友 std::ostream 方法进行输出。我已将它导入到头文件中,如下所示,但我不知道如何在学生中使用它.cpp。添加 student::ostream 不起作用。我如何能够在我的 student.cpp 中使用标头预定义方法

我的头文件

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

学生.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:: 的那个是否使用了我在头文件中定义的那个。

【问题讨论】:

  • 您的问题是什么?您似乎已经准备好所有代码。
  • 你的student.cpp中有一个正确的实现,你想在哪里使用它?你可能想在课堂之外使用它,例如Student s; std::cout &lt;&lt; s;
  • @SergeyA 我不知道如何使用我在头文件中定义的预定义输出方法。为了清楚起见,我将添加屏幕截图。
  • 头文件声明中的参数之间不需要逗号吗?
  • @RohanParikh 不,定义中不应该有student::friend 函数不是类成员,即使它可以在类定义中声明。这是一个免费的功能。而且必须是这样,否则任何运算符重载都需要类实例作为第一个参数,这意味着您需要编写student s; s &lt;&lt; std::cout;,这绝对会让所有阅读您的代码的人感到困惑。

标签: c++ visual-c++ header-files friend


【解决方案1】:

我会按照以下方式进行:

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

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玩游戏

【讨论】:

  • 1. add function settingStudentName in both header and main file 为什么这与问题的问题有关?
  • @t.niese 因为否则程序只会打印“有纪律问题”
  • 这很有帮助,谢谢!我认为不需要 main 方法,因为我有另一个名为 main.cpp 的文件,正确。我只需要在该文件中包含方法的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2012-05-15
  • 1970-01-01
  • 2016-01-08
相关资源
最近更新 更多