[英]error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
[英]Overloaded 'operator++' must be a unary or binary operator (has 3 parameters)
我有一個頭文件和一個.cpp文件。 我試圖實現前綴和后綴運算符重載,但我在設置重載時不斷收到此錯誤。
fraction.h
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction
{
public:
Fraction();
Fraction(int, int);
int getTop() {return m_top;}
int getBottom() {return m_bottom;}
void set(int t, int b) {m_top=t; m_bottom=b; reduce();
}
protected:
private:
void reduce();
int gcf(int, int);
int m_top;
int m_bottom;
};
Fraction& operator ++ (Fraction);
Fraction operator++(Fraction, int);
#endif
Main.cpp的
#include <iostream>
using namespace std;
#include "fraction.h"
int main {
cout << "The fraction is" << f;
cout << "The output of ++f is " << (++f) << endl;
cout << "The fraction is" << f;
cout << "The output of f++ is " << (f++) << endl;
cout << "The fraction is" << f;
return 0;
}
Fraction& Fraction::operator ++ (Fraction){
// Increment prefix
m_top += m_bottom;
return *this;
}
Fraction Fraction::operator ++ (Fraction, int){
//Increment postfix
}
這是我得到的兩個錯誤:
prefix error: "Parameter of overloaded post-increment operator must have type 'int' (not 'Fraction')"
postfix error: "Overloaded 'Operator++' must be a unary or binary operator (has 3 parameters)"
前綴錯誤實際上是我的ide錯誤嗎? 我知道后增量必須是'int',但我試圖做一個預增量。 我用xcode。
您將類外的運算符聲明為非類函數
Fraction& operator ++ (Fraction);
Fraction operator++(Fraction, int);
然而,你試圖像類成員函數一樣定義它們
Fraction& Fraction::operator ++ (Fraction){
// Increment prefix
m_top += m_bottom;
return *this;
}
Fraction Fraction::operator ++ (Fraction, int){
//Increment postfix
}
通過以下方式將它們聲明為類成員函數
class Fraction
{
public:
Fraction & operator ++();
Fraction operator ++( int );
//...
在這種情況下,例如preincrement運算符的定義可能看起來像
Fraction & Fraction::operator ++(){
// Increment prefix
m_top += m_bottom;
return *this;
}
或者將它們聲明為非類函數,它們是類的朋友,因為它們需要訪問類的私有數據成員
class Fraction
{
public:
friend Fraction & operator ++( Fraction & );
friend Fraction operator ++( Fraction &, int );
//...
在這種情況下,例如preincrement運算符的定義可能看起來像
Fraction & operator ++( Fraction &f ){
// Increment prefix
f.m_top += f.m_bottom;
return f;
}
您將函數聲明為自由函數
Fraction& operator ++ (Fraction);
Fraction operator++(Fraction, int);
但是你將它們定義為成員函數。
Fraction& Fraction::operator ++ (Fraction){ ... }
Fraction& Fraction::operator ++ (Fraction, int){ ... }
由於成員函數具有隱式this
參數,因此您的成員函數有三個參數( this
, Fraction
和int
)。
決定您是希望功能是免費還是成員。 如果您希望它們是免費的,那么將它們定義為免費而非成員。 如果您希望它們成為成員,則將它們聲明為成員並調整上面@crayzeewulf所述的聲明。
成員函數有一個隱式* this指針,它總是指向成員函數正在處理的類對象。 我們必須在友元函數版本中明確列出的參數(沒有* this指針)成為成員函數版本中的隱式* this參數。
嘗試使其成為非成員函數。然后您可以傳遞參數否則刪除參數。
int main() {
Fraction f;
cout << "The fraction is" << f;
cout << "The output of ++f is " << (++f) << endl;
cout << "The fraction is" << f;
cout << "The output of f++ is " << (f++) << endl;
cout << "The fraction is" << f;
return 0;
}
Fraction& Fraction::operator++ ()
{
// Increment prefix
m_top += 1;
return *this;
}
const Fraction Fraction::operator++ (int)
{
//Increment postfix
Fraction temp = *this;
++(*this);
return temp;
}
ostream& operator<<(ostream &os, const Fraction& f)
{
os << f.m_top << endl;
return os;
}
Fraction::Fraction(const Fraction& f)
{
m_top = f.m_top;
m_bottom = f.m_bottom;
}
Fraction::Fraction(int t, int b) :m_top(t), m_bottom(b){}
Fraction::Fraction() : m_top(1), m_bottom(1){}
class Fraction
{
public:
Fraction();
Fraction(int, int);
Fraction(const Fraction& f);
int getTop() { return m_top; }
int getBottom() { return m_bottom; }
void set(int t, int b) {
m_top = t; m_bottom = b; reduce();
}
Fraction& operator ++ ();
const Fraction operator++(int);
friend ostream& operator<<(ostream &os,const Fraction& f);
protected:
private:
void reduce();
int gcf(int, int);
int m_top;
int m_bottom;
};
#endif
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.