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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33338861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:06:40  来源:igfitidea点击:

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

c++xcodeoperator-overloadingpost-incrementpre-increment

提问by Josh Ryan

I have a header file and a .cpp file. I am trying to implement a prefix and postfix operator overload but I keep getting this error when setting up the overload.

我有一个头文件和一个 .cpp 文件。我正在尝试实现前缀和后缀运算符重载,但在设置重载时不断收到此错误。

fraction.h

分数.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
}

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? I know it must be 'int' for post-increment, but I am trying to do a pre-increment. I use xcode.

前缀错误实际上是我 ide 的错误吗?我知道后增量必须是“int”,但我正在尝试进行前增量。我使用 xcode。

采纳答案by Vlad from Moscow

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

在这种情况下,例如预增量运算符的定义看起来像

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

在这种情况下,例如预增量运算符的定义看起来像

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

回答by Raymond Chen

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 thisparameter, your member functions have three parameters (this, Fraction, and int).

由于成员函数有一个隐含的this参数,你的成员函数有三个参数(thisFraction,和int)。

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 的说明调整声明。

回答by Aditya Shekhawat

A member function has an implicit *this pointer which always points to the class object the member function is working on. 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 指针)在成员函数版本中变成了隐式 *this 参数。

Try making it a non member function.Then you can pass the parameter Otherwise remove the parameter.

尝试使其成为非成员函数。然后您可以传递参数否则删除参数。

回答by xiang yang

    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