I dont want to post my entire code because my problem is only with a constructor.
I've made a class called Money, with two member variables 'dollars' and 'cents'.
I made a 2-parameter constructor which accepts 2 ints, and assigns them to 'dollars' and 'cents' respectively. However, I have also made this 2-parameter constructor a DEFAULT constructor as I have set the parameters equal to 0. Therefore, if nothing gets passed in, 'dollars' and 'cents' will be initialized to 0.
At least, thats what I expected. But thats not happening. If I make an object without passing in arguments, then my program does not compile as it seems to be looking for a 0-parameter constructor.
main file:
1 2 3 4 5 6
|
// Make object using 2-parameter constructor
Money bank1(dollars, cents); // this works without a problem
cout << "Bank1 has " << bank1.getDollars() << " dollars and " << bank1.getCents() << " cents" << endl;
// Make object using default constructor
Money bank2; // here is where the problem occurs
|
.cpp file
1 2 3 4 5 6 7
|
Money::Money(int d=0, int c=0) // 'd' and 'c' set to 0 by default
{
dollars = d;
cents = c;
simplify(); // this is a separate function called, unrelated.
}
|
.h file
1 2 3 4 5 6 7 8 9 10 11
|
class Money
{
private:
int dollars;
int cents;
void simplify();
public:
// Constructor
Money(int, int);
// rest of the class follows...
};
|
Specifically, im getting the following error message:
"error: no matching function for call to Money::Money();"
Do I have a faulty understanding of how constructors work or something?