我有一个头文件和一个.cpp文件。我正在尝试实现前缀和后缀运算符重载,但是在设置重载时一直出现此错误。
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
}
这是我得到的两个错误:
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)"
前缀错误实际上是我的想法错误吗?我知道后增量必须为“ int”,但我正在尝试进行前增量。我使用xcode。
您已将类外的运算符声明为非类函数
Fraction& operator ++ (Fraction);
Fraction operator++(Fraction, int);
但是您试图像类成员函数一样定义它们
Fraction& Fraction::operator ++ (Fraction){
// Increment prefix
m_top += m_bottom;
return *this;
}
Fraction Fraction::operator ++ (Fraction, int){
//Increment postfix
}
都可以通过以下方式将它们声明为类成员函数
class Fraction
{
public:
Fraction & operator ++();
Fraction operator ++( int );
//...
并且在这种情况下,preprecrement运算符的定义看起来像]
Fraction & Fraction::operator ++(){
// Increment prefix
m_top += m_bottom;
return *this;
}
或将它们声明为类的非类函数,因为它们需要访问该类的私有数据成员
class Fraction
{
public:
friend Fraction & operator ++( Fraction & );
friend Fraction operator ++( Fraction &, int );
//...
并且在这种情况下,preprecrement运算符的定义看起来像]
Fraction & operator ++( Fraction &f ){
// Increment prefix
f.m_top += f.m_bottom;
return f;
}
您已将函数声明为自由函数
Fraction& operator ++ (Fraction);
Fraction operator++(Fraction, int);
但是您将它们定义为成员函数。
Fraction& Fraction::operator ++ (Fraction){ ... }
Fraction& Fraction::operator ++ (Fraction, int){ ... }
由于成员函数具有隐式的this
参数,所以成员函数具有三个参数(this
,Fraction
和int
)。
确定您是否希望这些功能免费或成为成员。如果您希望它们免费,则将它们定义为自由而不是成员。如果要让它们成为成员,则将它们声明为成员,并按照上面@crayzeewulf的说明调整声明。
成员函数具有隐式* this指针,该指针始终指向该成员函数正在处理的类对象。我们必须在好友函数版本(没有* this指针)中明确列出的参数在成员函数版本中变为隐式* this参数。
尝试使其成为非成员函数。然后可以传递参数否则,请删除该参数。
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