Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would proceed in the following manner:

  1. add body and declaration parameters to function settingStudentName in both header and main file;
  2. insert correct dependancies (#include);
  3. add a main() driver function;
  4. also, use correct syntax (friend std::ostream& operator << (std::ostream&, const student &);) lacked the comma , separator between the function arguments.

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

Here is the output:

Jack has a discipline issue

And here is a compiled version you can play with https://wandbox.org/permlink/mF49xQxkXs3M7n0M


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...