运算符重载和引用(未定义的引用错误)

时间:2015-10-23 05:40:34

标签: c++ operator-overloading undefined-reference

我正在练习运算符重载和引用。我复制了一个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:在函数Money::Money(double)': prog.cpp:(.text+0x35): undefined reference to Money :: dollarsPart(double)const'   prog.cpp :(。text + 0x47):未定义引用Money::centsPart(double) const' /home/CtpY88/cckh1W1a.o: In function运算符&gt;&gt;(std :: istream&amp;,Money&amp;)':   prog.cpp :(。text + 0x33d):未定义引用Money::dollarsPart(double) const' prog.cpp:(.text+0x350): undefined reference to Money :: centsPart(double)const'   collect2:错误:ld返回1退出状态

有人可以帮我解释一下。

1 个答案:

答案 0 :(得分:1)

您似乎忘了定义Money::centsPartMoney::dollarPartMondy::round成员函数。