如何使用头文件中预定义的Friend方法?

罗汉·帕里克(Rohan Parikh)

本质上,我的计算机科学老师让我使用朋友std :: ostream方法进行输出。如下所示,我已将其导入头文件中,但是我不知道如何在student.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 :::它表示无法解析该符号。我不确定没有学生的那一个::是否正在使用我在头文件中定义的那个。

林戈

我将以以下方式进行:

  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播放的编译版本

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何预定义项目中的头文件路径

来自分类Dev

如何在头文件中定义对象?(不使用它)

来自分类Dev

单个头文件中的命名空间和类及其方法定义如何在多个头文件或cpp文件中使用呢?

来自分类Dev

如何访问头文件中定义的常量?

来自分类Dev

使用头文件C中定义的结构

来自分类Dev

如何在自己的js文件中利用一个js文件中的预定义方法?

来自分类Dev

如何在头文件中定义嵌套映射以在 .cpp 中使用

来自分类Dev

在C ++中定义头文件

来自分类Dev

LibClang:使用其他头文件中的定义来解析头文件?

来自分类Dev

使用头文件的C ++中的多定义错误

来自分类Dev

如何在laravel中使用预定义的destroy方法

来自分类Dev

如何使用带有预定义常量和变量的echo写入文件,批处理中不包含空格?

来自分类Dev

如何找到定义ac函数的头文件?

来自分类Dev

头文件中定义的函数的原型错误

来自分类Dev

避免在头文件中定义模板

来自分类Dev

头文件中const变量的多个定义

来自分类Dev

在头文件中定义类是否正确?

来自分类Dev

C-头文件中的结构定义

来自分类Dev

头文件中的函数未定义

来自分类Dev

头文件中const变量的多个定义

来自分类Dev

在头文件中定义类是否正确?

来自分类Dev

头文件中定义的函数的原型错误

来自分类Dev

C-头文件中的结构定义

来自分类Dev

检索头文件中定义的结构列表

来自分类Dev

使用头文件的多定义错误

来自分类Dev

如何在头文件中声明结构并在.c文件中定义

来自分类Dev

在哪里以及如何定义成员变量?在头文件或实现文件中?

来自分类Dev

头文件中未定义方法和纯虚方法之间的区别

来自分类Dev

不使用预定义方法进行转换

Related 相关文章

热门标签

归档