c++ - Need help can't figure out why my header file function declarations won't work -
cant figure out code. how make piggybank& header work add functions in cpp file?
gives me error:
error 6 error c2040: 'piggybank::adddimes' : 'void (int)' differs in levels of indirection 'piggybank &(int)' h:\cosc1030\homework09\homework09\piggybank.cpp 36 1 homework09
#ifndef piggybank_h #define piggybank_h #include <iostream> using std::cout; using std::endl; class piggybank { public: piggybank(int pennies, int nickels, int dimes, int quarters); // return number of coins in bank int getpenniescount() const; int getnickelscount() const; int getdimescount() const; int getquarterscount() const; // add coins bank piggybank& addpennies(int p); piggybank& addnickels(int n); piggybank& adddimes(int d); piggybank& addquarters(int q); // withdraw coins bank, return number withdrawn int withdrawpennies(int p); int withdrawnickels(int n); int withdrawdimes(int d); int withdrawquarters(int q); void displaybalance() const; void breakthebank(); // display balance cash out (all counts zeroed). private: int pennies; int nickels; int dimes; int quarters; }; #endif #include "piggybank.h" piggybank::piggybank(int pennies, int nickels, int dimes, int quarters) { addpennies(pennies); addnickels(nickels); adddimes(dimes); addquarters(quarters); } int piggybank::getpenniescount() const { return pennies; } int piggybank::getnickelscount() const { return nickels; } int piggybank::getdimescount() const { return dimes; } int piggybank::getquarterscount() const { return quarters; } void piggybank::addpennies(int p) { pennies = (p >= 0) ? p : 0; } void piggybank::addnickels(int n) { nickels = (n >= 0) ? n : 0; } void piggybank::adddimes(int d) { dimes = (d >= 0) ? d : 0; } void piggybank::addquarters(int q) { quarters = (q >= 0) ? q : 0; }
the header piggybank& adddimes(int d);
provided,
you'll need change void
in implementation piggybank&
.
what should returned same object of adddimes
called,
ie. this
. this
pointer, return *this;
.
maybe wonder for: can write things
functionwhichtakesapiggybank(mypiggybank.adddimes(1)); //one line mypiggybank.adddimes(1).adddimes(1).adddimes(1); //chaining
etc.
Comments
Post a Comment