朋友不允许在 class 定义之外

friend not allowed outside of a class definition

我试图在赋值中加载 cout 运算符,我被迫并被要求将我的 class 拆分为(.h 和 .cpp)。这是我的完整代码:

instructor.h

#include "person.h"
#ifndef instructor_h
#define instructor_h
class instructor: public person {

private:    
    int children;
    int salary;
    string maritalStatus;

public:

    instructor();
    instructor(string, string, string, int , int ,string);

instructor operator++();

    void print();



    int calcSalary();

    int getSalary();
    int getNumOfChildren();
    string getMarialStatus();

    friend ostream &operator <<(ostream, instructor );
    void setSalary(int);
    void getNumOfChildren(int);
    void setMarialStatus(string);

};

#endif

instructor.cpp

#include <iostream>
#include <string>
#include "instructor.h"
using namespace std;

instructor::instructor() {

}


instructor::instructor(string a , string b, string c , int chil , int sal ,string mar):person(a,b,c) 

{
    children = chil;
    salary = sal;
    maritalStatus = mar;
}


instructor instructor::operator ++()
    {
        children=children+1;
        return *this;
    }


int instructor::calcSalary() {

    int new_sal;
    new_sal = salary + children*0.1;

    return new_sal;
}

int instructor::getSalary() {

    cout <<"Here is the result of your query:"<<endl;
    cout <<"================================="<<endl;
    cout<< "Salary: "<<salary<<""<<endl;
    cout <<"================================="<<endl;
    cout <<endl;

    return salary;


}

int instructor::getNumOfChildren() {

    cout <<"Here is the result of your query:"<<endl;
    cout <<"================================="<<endl;
    cout<< "Number of children: "<<children<<""<<endl;
    cout <<"================================="<<endl;


    cout <<endl;
    return children;


}

string instructor::getMarialStatus() {

    cout <<"Here is the result of your query:"<<endl;
    cout <<"================================="<<endl;
    cout<< "Marital Status: "<<maritalStatus<<""<<endl;
    cout <<"================================="<<endl;
    cout <<endl;

    return maritalStatus;

}


friend ostream& operator<<(ostream& os, instructor& v){
    os << v.children;

return os;
}



void instructor::setSalary(int sal) {

    salary = sal;
}

void instructor::getNumOfChildren(int nmc) {

    children = nmc;

}

void instructor::setMarialStatus(string sms) {

    maritalStatus = sms;

}



void instructor::print() {

    person::print();
    cout <<"Here is the result of your query:"<<endl;
    cout <<"================================="<<endl;
    cout<< "Marital Status: "<<maritalStatus<<""<<endl;
    cout<< "Number of children: "<<children<<""<<endl;
    cout<< "Salary: "<<salary<<""<<endl;
    cout <<"================================="<<endl;
    cout <<endl;


}

我收到以下错误:

instructor.cpp(75) : error C2255: 'friend' : not allowed outside of a class definition instructor.cpp(76) : error C2248: 'instructor::children' : cannot access private member declared in class 'instructor' 1>
c:\documents and settings\george\my documents\visual studio 2005\projects\hana\hana\instructor.h(7) : see declaration of 'instructor::children' 1>
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我正在使用 visual studio 2005。为什么会出现这些错误?

所有这些都是由于我试图重载运算符 cout 造成的:

friend ostream& operator<<(ostream& os, instructor& v){
    os << v.children;

return os;
}

为什么不起作用?

有两个问题。首先是您不能在 class 之外使用 friend,如错误所述。但是你不需要;在 class 中将函数声明为友元后,您只需在外部定义它,而不必再次提及友元性。

但另一个问题是你的声明和定义不匹配。您将此声明为好友:

ostream &operator <<(ostream, instructor );

然后你定义了这个:

ostream& operator<<(ostream&, instructor&)

它们不一样,但它们必须是一样的。