重载<<运算符错误C2804:二进制'运算符<<'参数太多
Overloading the << operator error C2804: binary 'operator <<' has too many parameters
这是我的类:
#ifndef CLOCK_H
#define CLOCK_H
using namespace std;
class Clock
{
//Member Variables
private: int hours, minutes;
void fixTime( );
public:
//Getter & settor methods.
void setHours(int hrs);
int getHours() const;
void setMinutes(int mins);
int getMinutes() const;
//Constructors
Clock();
Clock(int);
Clock(int, int);
//Copy Constructor
Clock(const Clock &obj);
//Overloaded operator functions
void operator+(const Clock &hours);
void operator+(int mins);
void operator-(const Clock &hours);
void operator-(int minutes1);
ostream &operator<<(ostream &out, Clock &clockObj); //This however is my problem where i get the error C2804. Saying that it has to many parameters
};
#endif
所有这些函数应该做的就是在不同的时间输出时钟的值。
ostream &operator<<(ostream &out, Clock &clockObj);
应该是
friend ostream &operator<<(ostream& out, Clock &clockObj);
在类外部定义。
请参见此处:是否应该使用运算符<lt;是作为朋友实现还是作为成员功能实现?
ostream &operator<<(ostream &out, Clock &clockObj);
应该是
friend ostream &operator<<(ostream &out, Clock &clockObj);
根据Stanley等人的C++Primer(第四版,第514页(:
当我们定义符合iostream库的约定,我们必须使其成为非成员操作人员我们不能使运算符成为我们自己类的成员。如果我们完成,则左侧操作数必须是类
因此,将<<
和>>
作为类的友元函数重载是一种很好的做法。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多