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

4

7

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.

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
}

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.

Calumet answered 26/10, 2015 at 5:12 Comment(1)
There are several issues in your code. Here are a few, fixing which might get you the answer. In fraction.h you declare a class named fraction but the increment operators are using a class named Fraction. In fraction.h you declare non-member versions of the two operators while in Main.cpp you are defining operators that are member functions of class Fraction. See this for details regarding inside class and outside class definitions of the operators.Propst
S
4

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;
}
Spathose answered 26/10, 2015 at 5:47 Comment(0)
N
0

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

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.

Nosedive answered 26/10, 2015 at 5:29 Comment(0)
F
0

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.

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

Fain answered 26/10, 2015 at 5:34 Comment(0)
E
-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
Enidenigma answered 26/10, 2015 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.