【发布时间】:2017-03-27 18:33:04
【问题描述】:
我正在尝试编写一个重载 operator<< 的类,但它一直给我这个错误。这是我的代码:
//Course.h
friend ostream& operator<<(ostream& os,Course& course);
//Course.cpp
ostream& operator<<(ostream& os,Course& course)
{
os << course.courseCode << " " << course.credit << " " << course.section " " << endl;
return os;
}
这是所有的 .h
#ifndef COURSE_H
#define COURSE_H
#include <string>
using namespace std;
class Course
{
public:
Course();
Course(string code,int credit,int section);
virtual ~Course();
string getCourseCode();
void setCourseCode(string code);
int getCredit();
void setCredit(int credit);
int getSection();
void setSection(int section);
bool operator==(Course &course);
bool operator!=(Course &course);
friend ostream& operator<<(ostream& os,const Course& course);
private:
string courseCode;
int credit,section;
};
#endif // COURSE_H
这是 .cpp 的一部分
#include "Course.h"
.
.
//Other functions' implementations
ostream& operator<<(ostream& os,const Course& course)
{
os << course.courseCode << " " << course.credit << " " << course.section " " << endl;
}
我将参数更改为const,但没有任何改变。
提前谢谢你。
【问题讨论】:
-
你在课堂上声明你的朋友了吗?
-
你能发布一个完整的 MWE,包括你如何称呼 operator
-
@Papipone 是的,它在我的课堂上。
-
@jwimberley 抱歉,MWE 是什么:/
-
@Mystro 以下链接描述了它是什么:stackoverflow.com/help/mcve