2013-10-08 89 views
2
#include"Fraction.h" 

#include<iostream> 

using namespace std; 


Fraction operator*(const Fraction& left, const Fraction& right) 
{ 
    int newNum = left.getNum() * right.getNum(); 
    int newDenom = right.getDenom() * left.getDenom(); 

    Fraction result(newNum, newDenom); //Error is here, cannot convert from Fraction to int 
    return result; 
} 

int main() 
{ 
    Fraction a(3,4); 
    Fraction b(1,2); 
    Fraction c = a * b; 

    cout << c << endl; 
} 

這是我的代碼在這裏,我們剛剛開始做運營商重載本週,我很卡住。運營商重載分數

主顯然不起作用,但這是我想要通過的輸入,但我不明白我得到的錯誤。任何人都可以爲我分解這一點嗎?我會很感激。

//This is my Header file for anyone that wants to see it 

#ifndef FRACTION_H 
#define FRACTION_H 

class Fraction 
{ 

public: 

    Fraction(int numParam, int denomParam); 

    void setNum(int numParam); 
    int getNum() const; 

    void setDenom(int denomParam); 
    int getDenom() const; 

private: 
    int num; 
    int denom; 

}; 

#endif 
+0

我竭力要看看如何代碼可能產生誤差。 – john

+0

我看不到這段代碼失敗。你可以告訴我們你的構造函數'Fraction(int,int)'嗎? – 0x499602D2

+3

我們需要看'Fraction.h'。 –

回答

3

它不知道如何顯示分數,補充一點:

std::ostream& operator<<(std::ostream& stream, const Fraction& frac) { 
    stream<<"Do something with frac here, like output num/denom\n"; 
    return stream; 
} 

雖然不抱怨你的cout行了,它會在你修復這個錯誤,因爲它不不知道如何在std :: ostream和Fraction上執行< <。

不是這個 - 從原來的答案

你的錯誤可能是那些「用在這裏」的錯誤標誌之一,它告訴你它在哪裏糊塗了,它可能試圖打印壓裂爲int .. ..是的。

實際ANSWER

當你說Fraction A = B*C;編譯器看到的像Fraction A = AnotherFraction;它可以做Fraction A;(默認構造函數),其次是Fraction::operator=(Fraction&);或者它可以從臨時使用在這種情況下的R值(移動分配)你回來。 C++允許一個級別的隱式轉換,在某處你有「Fraction :: Fraction(int);」 - 一個接受int的構造函數,C++想要將你的Fraction轉換爲一個int,並用該int構造一個新的Fraction。

它在抱怨,因爲它不能。給它一個拷貝構造函數,參見3的規則(現在的規則爲5),你應該總是擁有你的賦值操作符和拷貝構造函數,不要只有一個(沒有很好的理由),並且使用C++ 11賦值r運算符和「」構造函數。

在結束

,我們需要你的構造函數,即內隱鑄造一個級別是給你出現奇怪的錯誤,因爲「爲什麼INT」,但由於某種原因,C++要用分數::分數(INT) ,這我知道,從你所說的存在,這是不高興,因爲它不能去fraction-> INT(做就是了fraction->內部 - >部分)

注意

這是爲什麼C++有一個沉重的學習曲線,因爲它如此強大L(它與內隱的轉換確實是毫無疑問的好事!)你可以得到告訴你更多的地方得到了比什麼其實是錯誤的混淆錯誤。就像當你有一個運營商< <有一個錯字,並嘗試將其匹配的標準算< <的一個,你就像一個4頁的原因,爲什麼你說的是錯的,它怎麼能不實例化一個特定的(與人類觀點無關)模板。

附錄

template<class T> 
class Ratio { 
public: 
    Ratio(): top(0), bottom(1) {} 
    Ratio(T& from): top(from), bottom(1) {} 
    Ratio(T& top, T& bottom): top(top), bottom(bottom) {} 
    Ratio(T&& from): top(from), bottom(1) {} 
    Ratio(T&& top, T&& bottom): top(top), bottom(bottom) {} 
    Ratio& operator=(Ratio& from) { 
     top = from.top; 
     bottom = from.bottom; 
    } 
    Ratio& operator=(Ratio&& from) { 
     top = from.top; 
     bottom = from.bottom; 
    } 
    Ratio(const Ratio& from): top(from.top), bottom(from.bottom) {} 
    Ratio(Ratio&& from): top(from.top), bottom(from.bottom) {} 
    ~Ratio() {} 

    const T& getTop() const { return top; } 
    const T& getBottom() const { return bottom; } 
    T& getTop() { return top; } 
    T& getBottom() { return bottom; } 


    Ratio operator*(const Ratio& rhs) const { //we are the left hand side 
     T newTop = rhs.top*this->top; 
     T newBottom = rhs.bottom*this->bottom; 
     Ratio result(newTop,newBottom); 
     return result; 
    } 
private: 
    T top; 
    T bottom; 
}; 

template<class T> 
std::ostream& operator<<(std::ostream& stream, const Ratio<T>& ratio) { 
    stream<<ratio.getTop()<<"/"<<ratio.getBottom(); 
    return stream; 
} 

typedef Ratio<int> Fraction; 

int main(int,char**) { 
    Fraction a; 
    std::cout<<"A: "<<a<<"\n"; 
    Fraction b(1); 
    std::cout<<"B: "<<b<<"\n"; 
    Fraction c = a*b; 
    std::cout<<"A*B=C: "<<c<<"\n"; 
    Fraction d(5,3); 
    std::cout<<"Look! "<<d*d<<"\n"; 
    return 0; 
} 

這一工程!

+0

OP說錯誤來自'Fraction result(newNum,newDenom);'。 –

+0

@JohnSmith固定。 –

+0

非常感謝您的詳細回覆,此刻此刻我的頭腦一片譁然。我的構造函數是Fraction(int numParam,int denomParam),適合每個想看到它的人。 – user2757842

1

好的我想我知道問題是什麼。 在頭文件中,構造函數沒有被定義,所以當他運行程序時,它不會接受分數類採用兩個整數。 因此,通過定義構造函數,允許程序運行!

基本上這不是它。

Fraction::Fraction(int numeratorParam, int denomeratorParam) 
{ 

numerator = numeratorPram; 

denumerator = denumeratorParam; 

}