2017-02-27 82 views
-1

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 
+2

他們的意思是這不是產生這些錯誤消息的代碼,因爲消息引用了不在此代碼中的名稱。 –

+0

這不是問題,但是包含兩個連續下劃線('SICT_Fraction_H__')的名稱和以下劃線開頭並帶有大寫字母的名稱保留供實施使用。不要在你的代碼中使用它們。 –

+0

警告消息意味着已命名的函數返回對已銷燬變量的引用。不要這樣做。你不知道你會得到什麼。 – user4581301

回答

1

operator+只有當if語句是真事返回。它必須總是返回的東西。另一方面,operator*返回對局部變量temp的引用。它可能也應該返回值,如operator+

+0

呵呵?我不太明白。我該怎麼辦? – Zanonymous

+0

@贊匿什麼,_exactly_你不明白嗎? –

+0

我應該如何修改錯誤。 – Zanonymous

相關問題