Problem overloaded type multiplication

Hi guys,

I have an issue regarding multiplication of a constant. I have a one parameter constructor. Can someone please tell me why sol=scalar*fraction is not behaving as it should.

Thanks

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
35
class Fraction
{
public:
	Fraction(){}
	Fraction(int a):numerator(a), denominator(a){}
	Fraction(int a, int b):numerator(a), denominator(b){}
	void print(){
		cout<<numerator<<"/"<<denominator<<endl;
	}
	Fraction operator*(Fraction& f){
		Fraction temp;
		temp.numerator=numerator*f.numerator;
		temp.denominator=denominator*f.denominator;
		return temp;
	}
	
private:
	int numerator;
	int denominator;


};

int main()
{
	Fraction fraction(4,5);
	fraction.print();
	int scalar=5;

	Fraction sol=scalar*fraction;//problem here!!!!!
	sol.print();
	
	_getch();
	return 0;
}
What is the actual problem?
I don't think that you want to initialize both the numerator and the denominator with a when constructing with Fraction(int a).
I have created a fraction object, with a numerator and a denominator. On line 30 I am however trying to multiply a fraction object by an integer, not another fraction object.

After the code compiles, the solution should be 20/5.

Topic archived. No new replies allowed.