2015-10-23 38 views
-1

我正在練習運算符重載和引用。我複製了一個Money程序,但編譯時出錯。我不明白什麼是錯誤的意思,我不知道它在哪裏。運算符重載和引用(未定義的引用錯誤)

#include<iostream> 
#include<cstdlib> 
#include<cmath> 

using namespace std; 

class Money{ 
public: 
    Money(); 
    Money(double amount); 
    Money(int theDollars); 
    Money(int theDollars, int theCents); 
    double getAmount() const; 
    int getDollars() const; 
    int getCents() const; 

    friend const Money operator +(const Money& amount1, const Money& amount2); 
    friend const Money operator -(const Money& amount1, const Money& amount2); 
    friend bool operator ==(const Money& amount1, const Money& amount2); 
    friend const Money operator -(const Money& amount); 
    friend ostream& operator <<(ostream& outputStream, const Money& amount); 
    friend istream& operator >>(istream& inputStream, Money& amount); 

private: 
    int dollars; 
    int cents; 
    int dollarsPart(double amount) const; 
    int centsPart(double amount) const; 
    int round(double number) const; 
}; 

int main(){ 
Money yourAmount, myAmount(10,9); 

cout<<"Enter an amount of your Money: "; 
cin>>yourAmount; 

cout<<"Your amount is "<< yourAmount 
    <<endl 
    <<"My amount is "<< myAmount 
    <<endl; 

if(yourAmount==myAmount) 
    cout<<"We have the same amount of money.\n"; 
else 
    cout<<"One of us is richer.\n"; 

Money ourAmount=yourAmount+myAmount; 
cout<<yourAmount<<" + " <<myAmount <<" equals " <<ourAmount <<endl; 

Money diffAmount=yourAmount-myAmount; 
cout<<yourAmount<<" - " <<myAmount <<" equals " <<diffAmount <<endl; 

return 0; 
} 

Money::Money(): dollars(0), cents(0){} 

Money::Money(double amount): dollars(dollarsPart(amount)), cents(centsPart(amount)){} 

Money::Money(int theDollars): dollars(theDollars), cents(0){} 

Money::Money(int theDollars, int theCents): dollars(theDollars), cents(theCents){ 
if ((theDollars<0 && theCents>0)||(theDollars>0 && theCents<0)){ 
    cout<<"Inconsistent money data.\n"; 
    exit(1); 
} 
dollars=theDollars; 
cents=theCents; 
} 

double Money::getAmount() const{ 
return (dollars + cents*0.01); 
} 

int Money::getDollars() const{ 
return dollars; 
} 

int Money::getCents() const{ 
return cents; 
} 

const Money operator +(const Money& amount1, const Money& amount2){ 
int allCents1 = amount1.cents + amount1.dollars*100; 
int allCents2 = amount2.cents + amount2.dollars*100; 
int sumAllCents = allCents1 + allCents2; 
int absAllCents = abs(sumAllCents);   //modulus the amount of cents to avoid negative numbers 
int finalDollars = absAllCents/100;   //convert the amount of cents into dollars 
int finalCents = absAllCents%100;   //take the last two amount of cents as real cents 

if(sumAllCents<0){       //if the value is less than zero, it means negative amount of money 
    finalDollars = -finalDollars; 
    finalCents = -finalCents; 
} 

return Money(finalDollars, finalCents); 
} 

const Money operator -(const Money& amount1, const Money& amount2){  //pretty much same with operator +, just the minus sign and diff not sum 
int allCents1 = amount1.cents + amount1.dollars*100; 
int allCents2 = amount2.cents + amount2.dollars*100; 
int diffAllCents = allCents1 - allCents2; 
int absAllCents = abs(diffAllCents);   //modulus the amount of cents to avoid negative numbers 
int finalDollars = absAllCents/100;   //convert the amount of cents into dollars 
int finalCents = absAllCents%100;   //take the last two amount of cents as real cents 

if(diffAllCents<0){       //if the value is less than zero, it means negative amount of money 
    finalDollars = -finalDollars; 
    finalCents = -finalCents; 
} 

return Money(finalDollars, finalCents); 
} 

bool operator ==(const Money& amount1, const Money& amount2){ 
return ((amount1.dollars==amount2.dollars) && (amount1.cents==amount2.cents)); 
} 

const Money operator -(const Money& amount){ 
return Money(-amount.dollars, -amount.cents); 
} 

ostream& operator <<(ostream& outputStream, const Money& amount){ 
int absDollars = abs(amount.dollars); 
int absCents = abs(amount.cents); 

if(amount.dollars<0 || amount.cents<0)  //if the amount is less than zero, put negative sign 
    outputStream <<"$-"; 
else 
    outputStream <<"$"; 

outputStream << absDollars; 

if(absCents>=10) 
    outputStream <<'.' <<absCents; 
else 
    outputStream <<'.'<< '0' <<absCents; 

return outputStream; 
} 

istream& operator >>(istream& inputStream, Money& amount){ 
char dollarSign; 

inputStream >> dollarSign; 

if(dollarSign!='$'){ 
    cout<<"No dollar sign in Money input.\n"; 
    exit(1); 
} 

double amountAsDouble; 
inputStream >> amountAsDouble; 
amount.dollars = amount.dollarsPart(amountAsDouble); 
amount.cents = amount.centsPart(amountAsDouble); 

return inputStream; 
} 

併發症時,

/home/CtpY88/cckh1W1a.o: In function Money::Money(double)': prog.cpp:(.text+0x35): undefined reference to Money::dollarsPart(double) const' prog.cpp:(.text+0x47): undefined reference to Money::centsPart(double) const' /home/CtpY88/cckh1W1a.o: In function operator>>(std::istream&, Money&)': prog.cpp:(.text+0x33d): undefined reference to Money::dollarsPart(double) const' prog.cpp:(.text+0x350): undefined reference to Money::centsPart(double) const' collect2: error: ld returned 1 exit status

有人可以幫助我做一些解釋。

+0

你還沒有在任何地方定義'dollarsPart'。這就是錯誤的含義。 – juanchopanza

+0

可能的重複[什麼是未定義的引用/未解析的外部符號錯誤,以及如何解決它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-符號錯誤和如何-DO-修復) –

回答

1

看起來你忘了定義Money::centsPart,Money::dollarPartMondy::round成員函數。