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

I have a header file and a .cpp file. I try to implement the prefix and postfix operator overload, but I continue to get 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 } 

Here are two errors that 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 a prefix error an actual error with my idea? I know this must be an "int" for post-increment, but I'm trying to pre-increment it. I am using xcode.

+6
source share
4 answers

You declared operators outside the class as non-class functions

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

however then you try to define them as functions of a class member

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

Or declare them as member functions of a class as follows

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

Again, the definition of the preincrement operator example may look like

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

Or declare them as a function of nonclasses that are friends of the class, because they must have access to private members of the class data

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

Again, the definition of the preincrement operator example may look like

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

You have declared functions as free functions.

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

but you define 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 if you want the features to be free or members. If you want them to be free, define them as free, not as members. If you want them to be members, declare them as members and set up the ads as indicated in @crayzeewulf above.

0
source

A member function has an implicit * this pointer, which always points to an object of the class that the member function is working on. The parameter that we should explicitly specify in the version of the friend function (which does not have this pointer) becomes implicit * this parameter in the version of the member function.

Try to make it a non-member function. You can then pass the parameter. Otherwise, delete the parameter.

0
source
  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 
-2
source

Source: https://habr.com/ru/post/1234501/


All Articles