我正试图在任务中删除cout操作符,我被迫并要求将我的类拆分为(.h和.cpp)。这是我的完整代码:
#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
#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):错误C2255:'friend':不允许在类定义之外 instructor.cpp(76):错误C2248:'instructor :: children':不能 访问类'讲师'中声明的私有成员1&gt;
c:\ documents and settings \ george \ my documents \ visual studio 2005 \ projects \ hana \ hana \ instructor.h(7):见声明 '讲师::孩子'1&gt;
==========构建:0成功,1个失败,0个最新,0个跳过==========
我正在使用visual studio 2005.为什么我会收到这些错误?
所有这一切都是由于我尝试重载运算符cout:
friend ostream& operator<<(ostream& os, instructor& v){
os << v.children;
return os;
}
为什么不工作?
答案 0 :(得分:1)
有两个问题。第一个是你不能在课堂外使用friend
,正如错误所说的那样。但你不需要;已经将该功能宣布为班级中的朋友,您只需将其定义在外面,而无需再次提及朋友。
但另一个问题是您的声明和定义不匹配。你宣布这是朋友:
ostream &operator <<(ostream, instructor );
然后你定义了这个:
ostream& operator<<(ostream&, instructor&)
那些不一样,但它们必须是。