gpt4 book ai didi

c++ - 重载 << 运算符错误 C2804 : binary 'operator <<' has too many parameters

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:14 25 4
gpt4 key购买 nike

这是我的类(class):

#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);    

在类之外定义。

参见此处:Should operator<< be implemented as a friend or as a member function?

关于c++ - 重载 << 运算符错误 C2804 : binary 'operator <<' has too many parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com