Default construcotr not found? Program not compiling

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?
Last edited on
Not sure what's happening, but the default args in constructor works for me.

In second thought, you're specifying the default arg in IMPLEMENTATION, not in DECLARATION. That's your problem.
Last edited on
1
2
3
4
5
class Money {
    public:
        Money(int d = 0, int c = 0);
        // ...
};

Though, it would make more sense to do something like:
1
2
3
4
5
6
class Money {
    public:
        Money(); // default constructor
        Money(int d, int c); // value constructor
        // ...
};

That way you make sure that they have to specify either both arguments or neither, which isn't the case for the default parameter option.
Last edited on
Thats so weird... it works fine on the c++ shell.

Im working in codeBlocks, so is it a problem with codeblocks compiler?


In second thought, you're specifying the default arg in IMPLEMENTATION, not in DECLARATION. That's your problem.


EDIT: You were right. See my last 2 posts.
Last edited on
closed account (48T7M4Gy)
http://stackoverflow.com/questions/15786304/c-calling-private-function-in-a-class-from-the-public-section-of-that-class
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using std::cout;
using std::endl;

class Money
{
   private:
       int dollars;
       int cents;
    
    public:
        // Constructor
        Money(int, int);
        int getCents();
};

Money::Money(int d = 0, int c = 999) // 'd' and 'c' set to 0 by default
{
    dollars = d;
    cents = c;
}

int Money::getCents()
    return cents;
    
int main()
{
 Money bucks;
 std::cout << bucks.getCents() << endl;
 
 Money otherBucks(2, 7);
 std::cout << otherBucks.getCents() << endl;
}


999
7
 
Exit code: 0 (normal program termination)


@Kemort: Thats exactly what I have, but its not working for me on codeBlocks.

What did you give a link to that stackoverflow page? How is that related to my question?
Oh Snap! Liuyang was right!

I should have specified my default arguments in my .h file, and left them unspecified in my cpp file.

So:

.h file
1
2
3
4
5
6
7
8
9
10
11
12
class Money
{
   private:
       int dollars;
       int cents;
       void simplify();
   public:
        // Constructor
        Money(int d=0, int c=0); // 'd' and 'c' given default values HERE

// rest of the class follows...
};



cpp file:
1
2
3
4
5
Money::Money(int d, int c) //no default values specified here. 
{
    dollars = d;
    cents = c;
 }


This works now. Thank you so much Liuyang.
Last edited on
Apparently it works both ways in the c++ shell: Either the default arguments can be specified in the declaration OR in the definition.

In codeBlocks, the default arguments can ONLY be specified in the declaration. Otherwise it wont compile.
Last edited on
closed account (48T7M4Gy)
@Arslan
Thats exactly what I have

It's not.

but its not working for me on codeBlocks.

It has nothing to do with codeblocks.

What did you give a link to that stackoverflow page?
How is that related to my question?

Read it.

Liuyang picked up the same error of yours.
It's not.


How so? I have the same thing, including the getCents function (even though I didn't include it in the OP so as to keep it short).

It has nothing to do with codeblocks.


Im pretty sure it does. Like I said, code in the OP compiles in c++ shell, but not in codeblocks. Its a matter of syntactical difference between the compilers.

Read it.


I did read it. I read it when you linked it to me, and now I read it again, the question and answers provided. Still dont see what It has to do with my question.

Maybe if you could elaborate instead of typing out 2-word sentences and giving random links...

I am sorry, but your posts seem very condescending here. First you give me a random link without any explanation. Then you post the exact same code I have with the output, again without any English or explanation involved. Then you quote me and reply with 2-word sentences as if you dont have the time in the world to give a reasonable explanation of what you are talking about, yet expect me to completely understand you. And you refer me back to the same random link which I've read several times already but hasn't helped me.

Thank you again liuyang for being clear and concise.
Last edited on
closed account (48T7M4Gy)
How so?

Clearly the code I inserted is different from what you submiitted.

I have the same thing,

Clearly you don't.

including the getCents function

You're digging a hole.

(even though I didn't include it in the OP so as to keep it short).

It's as big as the Grand Canyon now.
Last edited on
Thank you for your input.


Topic archived. No new replies allowed.