-1

I was practising the operator overloading and references. I copied a Money program but there was error when compilation. I couldnt understand what is the error mean and i not sure where is it.

#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;
}

during complication,

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

can someone help me with some explanation.

m1031
  • 15
  • 1
  • 5
  • You haven't defined `dollarsPart` anywhere. That's what the error means. – juanchopanza Oct 23 '15 at 05:45
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – T3 H40 Oct 23 '15 at 05:52

1 Answers1

1

Looks like you forgot to define Money::centsPart, Money::dollarPart and Mondy::round member functions.

kjpus
  • 509
  • 4
  • 8