Class 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;
}
http://cplusplus.com/forum/articles/40071/#msg216270

Also, line 97 in the code you posted has a }...
This is a duplicate. Don't post duplicates. It makes a mess out of the forum. :-(
 
Fraction.operator*(n1,d1);

What are you trying to do here? First of all, you cannot call the operator * function like this; it must be called by an object of the Fraction class, not by the class name. Second, the operator * function does not change any of the operands used in the multiplication; it only returns the result of the multiplication, but the result is not assigned to anything in this statement.

Also, you have multiple default constructors, which my compiler at least does not like, and I'm pretty sure all compilers don't like that. Your constructor that has 2 parameters will set num to 0 and den to 1, so you can remove the constructor with 0 parameters.

On line 95, you have code that will evaluate to cin >> "/" and cin >> endl. These statements don't make any sense. Is this supposed to be a cout statement, or what are you trying to do here? Given the code you have, it looks like this should be a cin statement, but you can't put "/" or endl in those places then.

Your multFraction function has an int return type, but does not return anything. It either needs to be changed to void or actually return a value.

 
while(op==' '){

Again, what do you think this is supposed to do? Right now, your loop will never execute if the user inputs +, -, *, or /, or anything else that isn't a space, which is a problem because cin will completely ignore any spaces that the user inputs. So basically, the loop will NEVER execute, ever. Your cin >> op statement should use the cin.get function instead, because you only want to read in one character.

That's all I'll say for now; you've got quite a bit of stuff that needs work. Post back if you have more questions or if you get your program working.
Topic archived. No new replies allowed.