Classes Problem

I cant really understand why im getting a ambiguous error in line 97. Can anyone tell me what i did wrong and how to fix it?

Heres the code:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
#include<stdlib>
#include<time>
#include <string>
using namespace std;

class Fraction{
private:
	int num;
	int den;
public:
	void set(int n,int d);
	Fraction();
	Fraction(int n=0,int d=1);
	void reduce();
//	Fraction operator+(Fraction op2);
//	Fraction operator-(Fraction op2);
	Fraction operator*(Fraction op2);
//	Fraction operator/(Fraction op2);
	int operator==(Fraction op2);
	void display();
};

void Fraction::set(int n,int d){
	num=n;
	den=d;
}

Fraction::Fraction(){
	num=0;
	den=1;
}

Fraction::Fraction(int n,int d){
	num=n;
	den=d;
}

void Fraction::reduce(){
	int k,n;
	if(num<den)
		n=num/2;
	else
		n=den/2;
	for(k=n;k>=2;k--){
		if(num%k==0 && den%k==0){
			num=num/k;
			den=den/k;
		}
	}
}//end reduce

Fraction Fraction::operator*(Fraction op2){
	Fraction result(0,1);
	result.num=num*op2.num; 
	result.den=den*op2.den;
	result.reduce();
	return(result);
}

int Fraction::operator==(Fraction op2){
	if(num*op2.den==den*op2.num)
		return(1);
	else
		return(0);
}

void Fraction::display(){
	if(den!=1)
		cout<<num<<"/"<<den;
	else
		cout<<num;
}//end display

int multFraction(){
    srand(time(0));
    Fraction f1(0,1),f2(0,1);
    f1.set(rand()%15+1,rand()%15+1);
    f2.set(rand()%15+1,rand()%15+1);
    cout<<"Enter answer"<<endl;
    f1.display();
    cout<<"*";
    f2.display();
    cout<<"=";
   }

int main(){
    char op=' ';
    int n1,d1,;
    cout<<"What operation would you like to do?(+,-,* or /)"<<endl;
    cin>>op;
    while(op==' '){
                if(op=='*'){
                multFraction();
                cin>>n1>>"/">>d1>>endl;
                Fraction.operator*(n1,d1);
                            }
                   }
    Fraction f1(-6,12),f2(1,-2),f3(0,1);
    //f3=f1*f2;
    //f3.display();
    if(f1==f2)
              cout<<"equal"<<endl;
    else
              cout<<"not equal"<<endl;
    system("pause");
    return 0;
}
Last edited on
Edit your post and place code tags around the code, and indent it properly, please.

[code]//The code goes here. [/code]
So is there a fix to this.
Line 97 doesn't have much. Is it line 96 instead?

Several problems:

1. You are calling operator* as if it were a static function of class Fraction in line 96, and not even doing it properly (you use :: instead of a period).
2. You have ambiguous constructors for Fraction: Fraction() and Fraction(int num = 0, int den = 1). Get rid of the first one.
3. Operator* is a binary operator. It can take at most one argument, and you are passing 2.
4. Your only operator* is in line 53 and it takes another fraction as an argument, not an int (much less 2 ints).

There are probably other errors. Start correcting these first. Then post again if more help is needed.
Thanks!! I want to generate 2 fractions and the user types the answer. The program should compare the right answer with the users one to see if its correct.

The error im getting is cannot convert `Fraction' to `Fraction()()' in assingment.

Any suggestions?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<string>
using namespace std;

class Fraction{
private:
	int num;
	int den;
public:
	void set(int n,int d);
	Fraction();
	Fraction(int n=0,int d=1);
	void reduce();
//	Fraction operator+(Fraction op2);
//	Fraction operator-(Fraction op2);
	Fraction operator*(Fraction op2);
//	Fraction operator/(Fraction op2);
	int operator==(Fraction op2);
	void display();
};

void Fraction::set(int n,int d){
	num=n;
	den=d;
}

//Fraction::Fraction(){
//	num=0;
//	den=1;
//}

Fraction::Fraction(int n,int d){
	num=n;
	den=d;
}

void Fraction::reduce(){
	int k,n;
	if(num<den)
		n=num/2;
	else
		n=den/2;
	for(k=n;k>=2;k--){
		if(num%k==0 && den%k==0){
			num=num/k;
			den=den/k;
		}
	}
}//end reduce

Fraction Fraction::operator*(Fraction op2){
	Fraction result(0,1);
	result.num=num*op2.num;
	result.den=den*op2.den;
	result.reduce();
	return(result);
}

int Fraction::operator==(Fraction op2){
	if(num*op2.den==den*op2.num)
		return(1);
	else
		return(0);
}

void Fraction::display(){
	if(den!=1)
		cout<<num<<"/"<<den;
	else
		cout<<num;
}//end display

int multFraction(){
    srand(time(0));
    Fraction f1(0,1),f2(0,1),f3();
    f1.set(rand()%15+1,rand()%15+1);
    f2.set(rand()%15+1,rand()%15+1);
    cout<<"Enter answer"<<endl;
    f1.display();
    cout<<"*";
    f2.display();
    cout<<"=";
    f3=f1.operator*(f2);
    return(f3);
   }

int main(){
    char op=' ';
    int n,d;
    cout<<"What operation would you like to do?(+,-,* or /)"<<endl;
    cin>>op;
    while(op==' '){
                if(op=='*'){
                Fraction f4();            
                multFraction();
                cout<<"Enter num answer";
                cin>>n;
                cout<<"Enter den answer";
                cin>>d;
                Fraction.operator*(n,d);
                            }
                   }
    //Fraction f1(-6,12),f2(1,-2),f3(0,1);
    //f3=f1*f2;
    //f3.display();
    if(f3==f4)
              cout<<"equal"<<endl;
    else
              cout<<"not equal"<<endl;
    system("pause");
    return 0;
} 
closed account (zwA4jE8b)
When overloading operators you need to pass by reference.

 
Fraction operator*(Fraction &op2);


otherwise op2 is not initialized (or something like that)
closed account (zwA4jE8b)
Here is my rational class, maybe it will help

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Michael Ervin - Rational # header

#ifndef H_RATIONAL
#define H_RATIONAL
#include <iostream>
using namespace std;

class rational
{
protected:
	double _n, _d;
public:
	rational();
	rational(double, double );
	void setnum(double);
	void setden(double);
	double getnum();
	double getden();
	void printrat(ostream&);
	friend void redux(rational&);
	friend rational operator+ (rational&, rational&);
	friend rational operator- (rational&, rational&);
	friend rational operator* (rational&, rational&);
	friend rational operator/ (rational&, rational&);
	friend ostream& operator<< (ostream&, rational&);
	friend istream& operator>> (istream&, rational&);
};

rational::rational()
{
	_n = 0;
	_d = 1;
}

rational::rational(double _num, double _den)
{
	_n = _num;
	_d = _den;
}

void rational::setnum(double _num){_n = _num;}

void rational::setden(double _den){_d = _den;}

double rational::getnum() {return _n;}

double rational::getden() {return _d;}

void rational::printrat(ostream& _out){_out << "Numerator = " << _n << " and Denominator = " << _d << endl;}

void redux(rational& _temp)
{
	double _d, _t = 1;  
	_d = (_temp._n > _temp._d) ? _temp._d : _temp._n;
	
	if (_d < 0)
		_d *= -1;
	
	for (int i = _d; i >= 2; i--)
		if (std::fmod(_temp._n, i) == 0 && std::fmod(_temp._d, i) == 0)
			if (_t < i)
				_t = i;

	_temp._n /= _t;
	_temp._d /= _t;
	
	if (_temp._d < 0)
	{
		_temp._n *= -1;
		_temp._d *= -1;
	}
}

rational operator+ (rational& _left, rational& _right)
{
	double  _num, _den;

	_num = ((_left._n*_right._d)+(_right._n*_left._d));
	_den = (_left._d*_right._d);

	rational _temp(_num, _den);
	redux(_temp);
	return _temp;
}

rational operator- (rational& _left, rational& _right)
{
	double  _num, _den;
	_num = ((_left._n*_right._d)-(_right._n*_left._d));
	_den = (_left._d*_right._d);

	rational _temp(_num, _den);
	redux(_temp);
	return _temp;
}

rational operator* (rational& _left, rational& _right)
{
	rational _temp((_left._n*_right._n),(_left._d*_right._d));
	redux(_temp);
	return _temp;
}
rational operator/ (rational& _left, rational& _right)
{
	rational _temp((_left._n*_right._d),(_left._d*_right._n));
	redux(_temp);
	return _temp;
}

ostream& operator<< (ostream& _out, rational& _ratobj)
{
	if (_ratobj._d == 1)
		_out << _ratobj._n;
	else if (_ratobj._n == 0)
		_out << 0;
	else
		_out << _ratobj._n << "/" << _ratobj._d;
	return _out;
}

//Reads in numbers formatted - N/D
istream& operator>> (istream& _in, rational& _ratobj)
{
	char ch;
	_in >> _ratobj._n >> ch >> _ratobj._d;
	return _in;
}
#endif 
That's not really true.

Although it's best to pass by const reference. And for operators that don't change the state of the current object, it's best to make them const.

Don't neglect const correctness!

Fraction operator * (const Fraction& op2) const;
closed account (zwA4jE8b)
also, why are you calling
Fraction.operator*(n,d);

overloading the operator makes stuff like
1
2
Fraction a, b, c;
c = a*b;

possible.

f3=f1.operator*(f2); should be f3 = f1*f2

but maybe I just do not understand your syntax. I have never seen it like that before though
Topic archived. No new replies allowed.