错误:>在244_w5_lab_prof.cpp中包含的文件中:11:0:
Fraction.h:在成员函数'sict :: Fraction& sict :: Fraction :: operator *?>>>(sict :: Fraction)const':
Fraction.h:83:13:警告:引用局部变量'bb'返回[默认启用]
Fraction.h:在成员函数'sict :: Fraction sict :: Fraction :: operator +(sict :: Fraction)'中: Fraction.h:78:3:警告:控制到达非空函数的末尾[-Wreturn-type]
在Fraction.cpp中包含的文件中:12:0:
Fraction.h:在成员函数'sict :: Fraction& sict :: Fraction :: operator *>(sict :: Fraction)const':
Fraction.h:83:13:警告:引用局部变量'bb'返回[启用 默认]
#ifndef SICT_Fraction_H__
#define SICT_Fraction_H__
#include <iostream>
using namespace std;
namespace sict{
class Fraction{
private:
int num; // Numerator
int denom; // Denominator
int gcd(); // returns the greatest common divisor of num
and denom
int max(); // returns the maximum of num and denom
int min(); // returns the minimum of num and denom
public:
void reduce(); // simplifies a Fraction number by dividing the
// numerator and denominator to their greatest common
divisor
Fraction(); // default constructor
Fraction(int n , int d=1); // construct n/d as a Fraction
number
void display() const {
if (num < 0 || denom < -1)
cout << "Invalid Fraction Object!";
else if (denom == 1)
cout << num;
else {
cout<<num<<"/"<< denom;
}
}
bool isEmpty() const;
// member operator functions
// TODO: write the prototype of member operator+= function HERE
Fraction & operator+=(const Fraction & f) {
num = num*f.denom + denom*f.num;
denom = denom*f.denom;
reduce();
return *this;
}
// TODO: write the prototype of member operator+ function HERE
Fraction operator+(const Fraction & f) {
if (!(this->denom == -1 || f.denom == -1))
{
Fraction temp;
temp.num = num*f.denom + denom*f.num;
temp.denom = denom*f.denom;
temp.reduce();
return temp;
}
}
// TODO: write the prototype of member operator* function HERE
Fraction & operator*(const Fraction & f) const {
Fraction temp;
temp.num = num*f.num;
temp.denom = denom*f.denom;
temp.reduce();
return temp;
}
};
};
#endif
答案 0 :(得分:1)
operator+
仅在if语句为真时返回一些内容。它必须总是返回一些东西。
另一方面,operator*
返回对局部变量temp
的引用。它也应该按价值返回,例如operator+
。