# Conversion Constructor help

• 02-15-2010
AvtrOfWoe
Conversion Constructor help
Hello, I am writing a class for a mixed number, and I have all of the operator overloads working and such when it comes to other mixed number objects. But My conversion constructor for if just an int is entered is not working. If someone could take a quick peak and see if anything jumps out, it would be much appreciated.

Declaration:

Code:

Mixed(int w = 0);
Definition:

Code:

Mixed::Mixed(int w){

whole = w;
numerator = 0;
denominator = 1;
}

These are the operations I am trying to perform and getting a type conversion error:

Code:

Mixed x;

cout << "(x + 10) = " << x + 10 << '\n';
cout << "(x - 4) = " << x - 4 << '\n';
cout << "(x * -13) = " << x * -13 << '\n';
cout << "(x / 7) = " << x / 7 << '\n';

• 02-15-2010
I'd post all of your code, for now we can probably only guess various problems with your implementation (i.e., did you properly overload the "<<" operator of your class). Its also a good idea to post the exact error message (not the "type" of error message).
• 02-16-2010
iMalc
Quote:

Originally Posted by AvtrOfWoe
Hello, I am writing a class for a mixed number, and I have all of the operator overloads working and such when it comes to other mixed number objects. But My conversion constructor for if just an int is entered is not working. If someone could take a quick peak and see if anything jumps out, it would be much appreciated.

Declaration:

Code:

Mixed(int w = 0);
Definition:

Code:

Mixed::Mixed(int w){

whole = w;
numerator = 0;
denominator = 1;
}

These are the operations I am trying to perform and getting a type conversion error:

Code:

Mixed x;

cout << "(x + 10) = " << x + 10 << '\n';
cout << "(x - 4) = " << x - 4 << '\n';
cout << "(x * -13) = " << x * -13 << '\n';
cout << "(x / 7) = " << x / 7 << '\n';

For those operations to invoke your constructor that takes an int, you'd have to have written operator overloads of +, -, *, and /. Perferably the two-parameter friend version so that both sides are implicitly convertible. I would also bracket the calculations so that the operator precedence is more explicit.
You should also be using a "constructor initialisation list".