2014-10-03 202 views
3

任何人都可以解釋這個錯誤,爲什麼我得到它嗎?我相信我已經將所有的算術運算放在了我的單獨課程中。調用不帶對象參數的非靜態成員函數

該程序存儲和操縱分數,同時測試超載。

預先感謝您。

我想在驅動程序中測試這個看起來像這樣的輸出。

f1 = 3/6, f2 = 4/5 
f1 + f2 = 39/30 = 13/10 = 1.3 
f1 - f2 = -9/30 = -3/10 = -0.3 
f1 * f2 = 12/30 = 2/5 = 0.4 
f1/f2 = 15/24 = 5/8 = 0.625 
Enter a fraction [numerator denominator]: 2 5 
You entered 2/5 

我Fraction.cpp類

#include "Fraction.h" 

#include <iostream> 

using namespace std; 

Fraction::Fraction(const int numerator, const int denominator) { } 

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

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

int Fraction::gcd() const { 
    int n = numerator; 
    int d = denominator; 
    int temp; 

    while (d!=0) { 
     temp = n % d; 
     n = d; 
     d = temp; 
    } 

    return n; 

} 

void Fraction::setNumerator(const int numerator) { 
    this->numerator = numerator; 
} 

void Fraction::setDenominator(const int denominator) { 
    this->denominator = denominator; 
} 

string Fraction::toString() const { 
    return ""; 
} 

double Fraction::returnDecimal() const { 
    double a = (double) getNumerator(); 
    double b = (double) getDenominator(); 

    return (a/b); 
} 

void Fraction::reduce() { 

    int a = gcd(); 
    int b = numerator/a; 
    int c = denominator/a; 

    setNumerator(b); 
    setDenominator(c); 

} 

Fraction Fraction::operator +(const Fraction& f) const { 

    Fraction temp; 

    int a = getNumerator(); 
    int b = getDenominator(); 
    int c = f.getNumerator(); 
    int d = f.getDenominator(); 

    int tempOne = b; 
    int tempTwo = d; 

    a = a * tempTwo; 
    b = b * tempTwo; 

    c = c * tempOne; 
    d = d * tempOne; 

    temp.setNumerator(a+c); 
    temp.setDenominator(d); 

    return temp; 

} 

Fraction Fraction::operator -(const Fraction& f) const { 

    Fraction temp; 

    int a = getNumerator(); 
    int b = getDenominator(); 
    int c = f.getNumerator(); 
    int d = f.getDenominator(); 

    int tempOne = b; 
    int tempTwo = d; 

    a = a * tempTwo; 
    b = b * tempTwo; 

    c = c * tempOne; 
    d = d * tempOne; 

    temp.setNumerator(a-c); 
    temp.setDenominator(d); 

    return temp; 

} 

Fraction Fraction::operator *(const Fraction& f) const { 

    Fraction temp; 

    temp.setNumerator(getNumerator() * f.getNumerator()); 
    temp.setDenominator(getDenominator() * f.getDenominator()); 

    return temp; 

} 

Fraction Fraction::operator /(const Fraction& f) const { 

    Fraction temp; 

    temp.setNumerator(getNumerator() * f.getDenominator()); 
    temp.setDenominator(getDenominator() * f.getNumerator()); 

    return temp; 

} 

bool Fraction::operator ==(const Fraction& f) const { 

    return ((getNumerator() == f.getNumerator()) && (getDenominator() == f.getDenominator())); 

} 

bool Fraction::operator !=(const Fraction& f) const { 

    return !((getNumerator() == f.getNumerator()) && (getDenominator() == f.getDenominator())); 

} 

bool Fraction::operator <(const Fraction& f) const { 

    double a = getNumerator(); 
    double b = getDenominator(); 

    double c = getNumerator(); 
    double d = getDenominator(); 

    return ((a/b) < (c/d)); 

} 

bool Fraction::operator <=(const Fraction& f) const { 

    double a = getNumerator(); 
    double b = getDenominator(); 

    double c = getNumerator(); 
    double d = getDenominator(); 

    return ((a/b) <= (c/d)); 

} 

bool Fraction::operator >(const Fraction& f) const { 

    double a = getNumerator(); 
    double b = getDenominator(); 

    double c = getNumerator(); 
    double d = getDenominator(); 

    return ((a/b) > (c/d)); 

} 

bool Fraction::operator >=(const Fraction& f) const { 

    double a = getNumerator(); 
    double b = getDenominator(); 

    double c = getNumerator(); 
    double d = getDenominator(); 

    return ((a/b) >= (c/d)); 

} 

ostream& operator <<(ostream& out, const Fraction& f) { 
    out << f.getNumerator() << "/" << f.getDenominator(); 

    return out; 
} 

istream& operator >>(istream& in, Fraction& f) {  

    int a; 
    int b; 

    in >> a >> b; 

    f.setNumerator(a); 
    f.setDenominator(b); 

    return in; 

} 

我的主類。

#include <iostream> 
#include "Fraction.h" 
#include "Fraction.cpp" 
using namespace std; 

int main(int argc, const char * argv[]) 
{ 

    Fraction::getNumerator() const; 
    Fraction::getDenominator() const; 
    Fraction::gcd() const; 
    Fraction::setNumerator(<#const int numerator#>); 
    Fraction::setDenominator(<#const int denominator#>); 
    Fraction::toString(); 
    Fraction::returnDecimal() const; 
    Fraction::reduce(); 

    return 0; 
} 

Fraction.h

#include <iostream> 
#include <string> 

using namespace std; 

class Fraction { 

private: 

    int numerator; 

    int denominator; 

    int gcd() const; 

public: 

    Fraction(const int numerator = 0, const int denominator = 0); 

    int getNumerator() const; 

    int getDenominator() const; 

    void setNumerator(const int numerator); 

    void setDenominator(const int denominator); 

    string toString() const; 

    double returnDecimal() const; 

    void reduce(); 

    bool operator ==(const Fraction& f) const; 

    bool operator !=(const Fraction& f) const; 

    bool operator <(const Fraction& f) const; 

    bool operator <=(const Fraction& f) const; 

    bool operator >(const Fraction& f) const; 

    bool operator >=(const Fraction& f) const; 

    Fraction operator +(const Fraction& f) const; 

    Fraction operator -(const Fraction& f) const; 

    Fraction operator *(const Fraction& f) const; 

    Fraction operator /(const Fraction& f) const; 

    friend ostream& operator <<(ostream&, const Fraction&); 

    friend istream& operator >>(istream&, Fraction&); 

}; 
+1

main()中代碼的用途是什麼?你能解釋一下你想讓你的程序使用Fraction類嗎? – 2014-10-03 03:14:08

+0

如果沒有'Fraction.h'文件,'Fraction.cpp'文件是沒用的。 – EJP 2014-10-03 03:14:53

+0

我有一個Fraction.h文件,我只是沒有把它添加到我的文章,並不認爲這很重要。 – MatthewTingle 2014-10-03 03:15:59

回答

9

分數是一個類。你需要該類的一個對象來調用它的(非靜態)函數。

Fraction f; 
f.getNumerator(); 
+0

對於gcd()和setNumerator()和setDenominator()呢? – MatthewTingle 2014-10-03 03:20:04

+2

所有功能的情況都是一樣的。你需要更多地研究一個類和一個對象之間的區別。 – 2014-10-03 03:24:05

+0

我意識到分數是一個類,我的教授只是說,我們通常應該總是在另一個類的調用之前有Fraction ::。或無論你有什麼要求: – MatthewTingle 2014-10-03 03:32:20

相關問題