2013-07-12 40 views
4

所以基本上我正在嘗試做一個分數類。它將採用用戶輸入的分數並執行加法。例如,我輸入1 5和1 7,添加它將打印出12/35。
這裏是我的.H類:在C++中添加餾分

#include <string> 
#ifndef _FRACTION_H_ 
#define _FRACTION_H_ 

using namespace std; 

class Fraction 
{ 
    public: 
     Fraction(); 
     Fraction(int n, int d); 
     int getNumerator() const; 
     int getDenominator() const; 
     void display(); 
     string to_string(); 
     Fraction operator+(Fraction &second); 
    private: 
     int numerator; 
     int denominator; 
}; 

這是我的.cpp文件

#include "Fraction.h" 
#include <string> 
include <iostream> 

using namespace std; 

Fraction::Fraction(){} 

Fraction::Fraction(int n, int d) 
{ 
    this->numerator = n; 
    this->denominator = d; 
} 

int Fraction::getNumerator() const 
{ 
    return numerator; 
} 

int Fraction::getDenominator() const 
{ 
    return denominator; 
} 

Fraction Fraction::operator+(Fraction &second) 
{ 
    int n1 = getNumerator() * second.getDenominator(); 
    int n2 = second.getNumerator() * getDenominator(); 
    int d = getDenominator() * second.getDenominator(); 
    return Fraction(n1+n2, d); 
} 
string Fraction::to_string() 
{ 
    return (getNumerator() + "/" + getDenominator()) ; 
} 

這是我主要方法:

bool get_input(Fraction &fract); 

int main() 
{ 
    Fraction fraction1, fraction2; 
    if (((!get_input(fraction1)) || (!get_input(fraction2)))) 
    cout << "Invalid Input!" << endl; 
    else 
    { 
     // Test harness for Arithmetic Operator Overloading 
     Fraction result = fraction1 + fraction2; 
     cout << "Addition = " << result.to_string() << endl; 
    } 

    bool get_input(Fraction& fract) 
    { 
     int num, den; 
     cout << "Enter numerator & denominator (separated by space)" << endl; 
     cin >> num >> den; 
     if (cin.fail()) 
     return false; 
     Fraction f(num,den); 
     fract = f; 
     return true; 
    } 
} 

它設法接受用戶i NPUT。但是,它不會打印出結果。提前致謝。

+2

您正在使用[保留標識符](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier)。請爲所有人起見,**不要**在標題中放置'using namespace ...;'。另外,你的'operator +'不能用於臨時或常量對象,這是意想不到的。 – chris

+0

對不起這是什麼意思?我該如何修復我的代碼?我認爲我的toString方法出錯了 –

+3

它不打印出結果?它有什麼作用?你是否已經用調試器完成了它? –

回答

6

有可能其他的問題,但Fraction::to_string() 功能顯然是錯誤的:在return表達 的類型是intchar const*int。添加這些 的結果是char const*,但假設字符串文字只有 兩個字符長,如果兩個int的總和大於 兩個,則表明您有未定義的行爲。您需要先將int 轉換爲字符串;要做到這一點最簡單的方法是使用 std::ostringstream

std::string 
Fraction::to_string() 
{ 
    std::ostringstream results; 
    results << numerator << '/' << denominator; 
    return results.str(); 
} 

通常情況下,人們所期望的編譯錯誤,當你濫用 類型這樣的,但由於歷史的原因:字符串字面 有型char const[],不std::string; char const[] 幾乎無處不在轉換爲char const*; C++支持 向指針添加整數值;和std :: string有 一個構造函數,它從char const*進行隱式轉換。

+0

但它告訴我不完整的類型是不允許的 –

+1

@Gwen,'#include '。 – chris

+0

即將推出字符串文字'''後綴,我感到非常放心。 – chris

1

您可以使用「+」在您的to_string法文本("/")和int 編譯器試圖做一些隱式轉換之間,運營商將其與point(INT +智力+字符*結束了,最好的猜測是的char *),然後使用正確的構造變成了一個的std :: string

更改方法使用stringstream(你就會對格式更精細的控制):

string Fraction::to_string() 
{ 
    std::stringstream s; 
    s << getNumerator() << "/" << getDenominator(); 
    return s.str(); 
} 

參考:http://www.cplusplus.com/reference/sstream/stringstream/

+0

oups,看起來我太慢了 – Bruce