C++ 不允许在类定义之外使用友元
我试图在作业中重新加载cout运算符,我被迫将类拆分为(.h和.cpp)。这是我的全部代码: 讲师C++ 不允许在类定义之外使用友元,c++,operator-overloading,C++,Operator Overloading,我试图在作业中重新加载cout运算符,我被迫将类拆分为(.h和.cpp)。这是我的全部代码: 讲师 #包括“person.h” #ifndef讲师 #定义讲师 班主任:公众人士{ 私人: 智力儿童; 国际工资; 字符串状态; 公众: 讲师(); 讲师(字符串,字符串,字符串,int,int,string); 讲师操作员++(); 作废打印(); int calcSalary(); int getSalary(); int getNumOfChildren(); 字符串getMarialStatu
#包括“person.h”
#ifndef讲师
#定义讲师
班主任:公众人士{
私人:
智力儿童;
国际工资;
字符串状态;
公众:
讲师();
讲师(字符串,字符串,字符串,int,int,string);
讲师操作员++();
作废打印();
int calcSalary();
int getSalary();
int getNumOfChildren();
字符串getMarialStatus();
friend ostream&operator有两个问题。第一个问题是,正如错误所说,您不能在类外使用friend
。但是您不需要这样做;在类内声明函数为friend之后,您只需在类外定义它,而不必再次提及friend ness
但另一个问题是您的声明和定义不匹配。您将此声明为朋友:
ostream &operator <<(ostream, instructor );
ostream&operator从.cpp文件中删除friend
你与朋友分享你的秘密,但你选择了你的朋友。你不一定与自称为朋友的人分享你的秘密。汤姆除外。他是每个人的朋友。现在,你似乎有了一个答案:有点按照惯例,讲师::operator++()应该返回对递增对象的引用。您正在返回一个副本“我正在尝试重新加载cout运算符”--否。std::cout
不是运算符;它是对象。您试图重载的是流插入器运算符
#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;
}
friend ostream& operator<<(ostream& os, instructor& v){
os << v.children;
return os;
}
ostream &operator <<(ostream, instructor );
ostream& operator<<(ostream&, instructor&)