2015-10-26 21 views
5

私はヘッダファイルと.cppファイルを持っています。接頭辞と後置演算子のオーバーロードを実装しようとしていますが、オーバーロードを設定するときにこのエラーが発生します。オーバーロードされた '演算子++'は単項演算子または2項演算子でなければなりません(3つのパラメータを持ちます)

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 
} 

これらは私が得る2つのエラーです:

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を使用します。

+0

のように見えることができますあなたのコードの中にあります。ここにあなたの答えを得るかもしれないいくつかの修正があります。 'fraction.h'では、' fraction'という名前のクラスを宣言しますが、インクリメント演算子は 'Fraction'という名前のクラスを使用しています。 'fraction.h'では2つの演算子のメンバでないバージョンを宣言し、' Main.cpp'では 'Fraction'のメンバ関数である演算子を定義しています。演算子の内部クラスと外部クラスの定義については、[this](http://en.cppreference.com/w/cpp/language/operator_incdec)を参照してください。 – crayzeewulf

答えて

2

あなたはしかし、あなたはどちらのクラスのメンバ関数のように

Fraction& Fraction::operator ++ (Fraction){ 
    // Increment prefix 
    m_top += m_bottom; 
    return *this; 
} 

Fraction Fraction::operator ++ (Fraction, int){ 
    //Increment postfix 
} 

それらを定義したクラスのメンバ関数として宣言しようとしている非クラス関数

Fraction& operator ++ (Fraction); 
Fraction operator++(Fraction, int); 

としてクラスの外の演算子を宣言次のようにしてください。

class Fraction 
{ 
public: 
    Fraction & operator ++(); 
    Fraction operator ++(int); 
    //... 

この場合、前置インクリメント演算子の例は

Fraction & Fraction::operator ++(){ 
    // Increment prefix 
    m_top += m_bottom; 
    return *this; 
} 

ように見えるか、彼らはクラスのプライベートデータメンバー

class Fraction 
{ 
public: 
    friend Fraction & operator ++(Fraction &); 
    friend Fraction operator ++(Fraction &, int); 
    //... 

とにアクセスする必要があるため、クラスの友達である非クラスの関数として宣言することができますこの場合には、前置インクリメント演算子の例の定義は、いくつかの問題があります

Fraction & operator ++(Fraction &f){ 
    // Increment prefix 
    f.m_top += f.m_bottom; 
    return f; 
} 
0

あなたは無料の機能として

Fraction& operator ++ (Fraction); 
Fraction operator++(Fraction, int); 

を関数を宣言していますがメンバ関数として定義されています。

Fraction& Fraction::operator ++ (Fraction){ ... } 
Fraction& Fraction::operator ++ (Fraction, int){ ... } 

メンバ関数は、暗黙のthisパラメータを持っているので、あなたのメンバ関数は、三つのパラメータ(thisFraction、およびint)を持っています。

機能をフリーまたはメンバにするかどうかを決定します。それらを自由にするには、メンバーではなく自由であると定義します。それらをメンバーにしたい場合は、それらをメンバーとして宣言し、上記の@crayzeewulfに記載されているように宣言を調整します。

0

メンバー関数には、メンバー関数が処理しているクラスオブジェクトを常に指す暗黙の* thisポインタがあります。 friend関数バージョン(* thisポインタを持たない)で明示的に指定しなければならないパラメータは、メンバ関数バージョンの暗黙の* thisパラメータになります。

非メンバ関数にしてみてください。 を渡すことができます。それ以外の場合はパラメータを削除してください。

-2
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 
関連する問題