error:>In file included from 244_w5_lab_prof.cpp:11:0:
Fraction.h: In member function ‘sict::Fraction& sict::Fraction::operator*?>>>(sict::Fraction) const’:
Fraction.h:83:13: warning: reference to local variable ‘bb’ returned [enabled by default]
Fraction.h: In member function ‘sict::Fraction sict::Fraction::operator+(sict::Fraction)’:
Fraction.h:78:3: warning: control reaches end of non-void function [-Wreturn-type]
In file included from Fraction.cpp:12:0:
Fraction.h: In member function ‘sict::Fraction& sict::Fraction::operator*>(sict::Fraction) const’:
Fraction.h:83:13: warning: reference to local variable ‘bb’ returned [enabled by default]我不知道這些錯誤消息意味着什麼
#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
他們的意思是這不是產生這些錯誤消息的代碼,因爲消息引用了不在此代碼中的名稱。 –
這不是問題,但是包含兩個連續下劃線('SICT_Fraction_H__')的名稱和以下劃線開頭並帶有大寫字母的名稱保留供實施使用。不要在你的代碼中使用它們。 –
警告消息意味着已命名的函數返回對已銷燬變量的引用。不要這樣做。你不知道你會得到什麼。 – user4581301