conversion constructor issue

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:

 
Mixed(int w = 0);


Definition:

1
2
3
4
5
6
7
8

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:

1
2
3
4
5
6
7
8

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';
Nothing is wrong with your constructor.

And you say you have +, -, etc operators overloaded?

what about the ostream << operator?
Yup, all of those operators are overloaded. And they all work, as long as the other parameter is a Mixed object
Hrm... the only thing I can think of is you're overloading one of them wrong.

Can you post your + and << operators?
sure, there are just a couple of extra functions I used in those.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Mixed operator+(Mixed& f1, Mixed& f2){
// this is the + operator overload

	Mixed r;

	f1.ToFraction();
	f2.ToFraction();

	// load result Mixed with sum of adjusted numerators
   r.numerator = (f1.numerator*f2.denominator)
                  + (f2.numerator*f1.denominator);

    // load result with the common denominator
   r.denominator = f1.denominator * f2.denominator;

   f1.Simplify();
   f2.Simplify();
   r.Simplify();
   
   return r;         // return the result Fraction



}


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
ostream& operator << (ostream& s, Mixed& m){
// This is an operator overload for the insertion ( << ) operator


	if (m.denominator < 0){

		m.denominator = m.denominator * -1;
		m.whole = m.whole * -1;
	}
	if (m.numerator < 0 && m.whole < 0){

		m.numerator = m.numerator * -1;
	}
	
	if (m.whole == 0){

		s << m.numerator << '/' << m.denominator;

	}

	else {

		s << m.whole << ' ' << m.numerator << '/' << m.denominator;
	}


	return s;


}



These are the functions used in the above operators. The ToFraction() function converts the Mixed number into an improper fraction. The Simplify() function converts improper functions back into mixed numnbers, as well as reduces fractions.

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
void Mixed::Simplify(){

	// This function converts an improper fraction back into a mixed number and reduces the fraction

	

	// If it is an improper fraction
	if (whole == 0){

	whole = numerator / denominator;

	numerator = numerator % denominator;

	}	

   int k, i;

   if(numerator < denominator){

        k = denominator;
   }
   else{
        k = numerator;
   }

   for (i = k; i > 0; i--){
        if(numerator % i == 0 && denominator % i == 0){

                numerator = numerator / i;
                denominator = denominator / i;
        }
   }



1
2
3
4
5
6
7
8
9
10
11
12
void Mixed::ToFraction(){
	// This Function converts the mixed number into an improper fraction

	int temp;

	temp = denominator * whole;

	numerator = temp + numerator;

	whole = 0;

}



And thank you for the help
ah, yep

That's the problem. You're not const correct. Make a few changes:


1
2
3
4
5
Mixed operator+(const Mixed& f1, const Mixed& f2){

ostream& operator << (ostream& s, const Mixed& m){

//etc 



EDIT: actually you might not be able to do that because your operators are modifying the objects passed to them.... hang on...


EDIT 2:

yeah you have to be const correct. +, -, etc operators should not be modifying the passed parameters. You'll have to redesign those functions slightly.

Here's a different way to do the + operator. You could change your other operators to do it a similar way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mixed operator+(const Mixed& f1, const Mixed& f2){

	Mixed r(f1);
	
	r.numerator   *= f2.denominator;
	r.denominator *= f2.denominator;
	
	r.numerator   += f2.numerator * f1.denominator;
	r.whole       += f2.whole;
	
	r.Simplify();
	
	return r;
}


But this also expects more out of Simplify.... You might end up with something like 3 5/4 (which you would want to simplify to 4 1/4, but it doesn't look like your Simplify would cover that)
Last edited on
Well, I couldnt do EXACTLY that, because in order to do the math, I had to change the Mixed number to a fraction. But I just created a copy and worked with that in the function, and it works perfectly now. Thank you very much for your ideas and help, I really appreciate it.
haha, I hadnt refreshed the page by the time you had suggested that, but once again, I really do appreciate it!
Topic archived. No new replies allowed.