操作员不匹配<<
No match for operator<<
我正在尝试编写一个带有重载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
但没有任何变化。
提前谢谢你。
下面是有关如何重载通量运算符的示例:
#include <iostream>
class Test
{
public:
Test(int a, int b) : a(a), b(b) {}
friend std::ostream& operator<<(std::ostream& stream, const Test& t);
private:
int a;
int b;
};
std::ostream& operator<<(std::ostream& stream, const Test& t)
{
stream << "Test::a " << t.a << "nTest::b " << t.b << 'n';
return stream;
}
int main()
{
Test t(20, 40);
std::cout << t;
return 0;
}
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 模板专用化与派生类不匹配