Confused about Constructor def

I'm writing a constructor for a class and I am confused about what to do. Here is the description for what the constructor should be:

Only one constructor with default value for Numerator = 0, Denominator = 1; But it should construct the Fraction numbers with, 1) no argument, 2) 1 argument, 3) 2 arguments.

I went ahead and wrote this in my header file:

1
2
3
Fraction(int = 0, int = 1);
Fraction(int);
Fraction(int, int);


and implemented it in my .cpp so that when there is only 1 int, it applies to the numerator, and when 2 ints are given, the first int applies to the numerator and the 2nd int applies to the denominator.

I am getting an error when running that member function is already defined or declared and ambiguous call to overloaded function. I have no idea what those mean or if I'm even following the instructions correctly. Can anyone tell me what it is that I am doing wrong?
You only need the one constructor as it asks:

 
Fraction(int = 0, int = 1);


You can use that one constructor as follows:

1
2
3
Fraction myfraction1;
Fraction myfraction2(1);
Fraction myfraction3(1,2);


When you don't supply an argument it uses the default values (0,1). When you supply one argument, that is passed to the first argument variable, and when you supply two ...
As above, you need only one.

Unfortunately (like the Highlander), there can be only that one, since it qualifies for the other 2 cases. There's no way of deciding which one to use - that's why you get the error message.

As you can see, 1 and 3 are actually identical, and 1 could also work in place of 2.

1 is okay
2 is ambiguous
3 has already been defined

Also realise that only the end parameters can be default, so using this method, you can't specify a default for the first parameter (I'm guessing that's the numerator), without defaulting the denominator also.
2 is ambiguous

Constructor 2 will no longer be ambiguous if you remove the default argument for the first parameter in constructor 1.
@Peter87

If you mean:
1
2
Fraction(int, int = 1);
Fraction(int);

It still would be, and now you've introduced a 4th constructor.
Yes, you are right. I thought constructor 2 had zero parameters, which was wrong thinking of me.
Last edited on
Oh okay I see. I feel silly now. Thanks guys.
Topic archived. No new replies allowed.