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?
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 ...
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.