简体   繁体   English

重载'operator ++'必须是一元或二元运算符(有3个参数)

[英]Overloaded 'operator++' must be a unary or binary operator (has 3 parameters)

I have a header file and a .cpp file. 我有一个头文件和一个.cpp文件。 I am trying to implement a prefix and postfix operator overload but I keep getting this error when setting up the overload. 我试图实现前缀和后缀运算符重载,但我在设置重载时不断收到此错误。

fraction.h 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 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
}

These are the two errors I get: 这是我得到的两个错误:

 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)"

Is the prefix error actually an error with my ide? 前缀错误实际上是我的ide错误吗? I know it must be 'int' for post-increment, but I am trying to do a pre-increment. 我知道后增量必须是'int',但我试图做一个预增量。 I use xcode. 我用xcode。

You declared the operators outside the class as non-class functions 您将类外的运算符声明为非类函数

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

however then you are trying to define them like class member functions 然而,你试图像类成员函数一样定义它们

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

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

Either declare them as class member functions the following way 通过以下方式将它们声明为类成员函数

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

And in this case the definition for example of the preincrement operator can look like 在这种情况下,例如preincrement运算符的定义可能看起来像

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

Or declare them as non-class function that are friends of the class because they need to have access to private data members of the class 或者将它们声明为非类函数,它们是类的朋友,因为它们需要访问类的私有数据成员

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

And in this case the definition for example of the preincrement operator can look like 在这种情况下,例如preincrement运算符的定义可能看起来像

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

You declared the functions as free functions 您将函数声明为自由函数

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

but you are defining them as member functions. 但是你将它们定义为成员函数。

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

Since member functions have an implicit this parameter, your member functions have three parameters ( this , Fraction , and int ). 由于成员函数具有隐式this参数,因此您的成员函数有三个参数( thisFractionint )。

Decide whether you want the functions to be free or member. 决定您是希望功能是免费还是成员。 If you want them to be free, then define them as free rather than member. 如果您希望它们是免费的,那么将它们定义为免费而非成员。 If you want them to be member, then declare them as member and adjust the declarations as noted by @crayzeewulf above. 如果您希望它们成为成员,则将它们声明为成员并调整上面@crayzeewulf所述的声明。

A member function has an implicit *this pointer which always points to the class object the member function is working on. 成员函数有一个隐式* this指针,它总是指向成员函数正在处理的类对象。 The parameter we had to list explicitly in the friend function version (which doesn't have a *this pointer) becomes the implicit *this parameter in the member function version. 我们必须在友元函数版本中明确列出的参数(没有* this指针)成为成员函数版本中的隐式* this参数。

Try making it a non member function.Then you can pass the parameter Otherwise remove the parameter. 尝试使其成为非成员函数。然后您可以传递参数否则删除参数。

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM