I'm having issues with the last part of my code. I have to add the declaration of void multiply(Fraction,Fraction); to my Fraction class, but when doing so, I get this error in my class implementation file: error: no 'void Fraction::multiply(Fraction, Fraction)' member function declared in class 'Fraction'.
Before this, my program executed fine. Other than adding that member function, nothing else has been altered.
#include "Fraction.h"
#include <iostream>
usingnamespace std;
//Constructor initializes the numerator and denominator
Fraction::Fraction()
{
numerator = 0;
denominator = 0;
}
//Constructor accepts arguments for numerator and denominator
Fraction::Fraction(int num,int den)
{
numerator = num;
denominator = den;
}
//setNumerator assigns a value to the numerator member
void Fraction::setNumerator(int n)
{
numerator = n;
}
//setDenominator assigns a value to the denominator member
void Fraction::setDenominator(int d)
{
denominator = d;
}
//getNumerator returns the value in the numerator member
int Fraction::getNumerator() const
{
return numerator;
}
//getDenominator returns the value in the denominator member
int Fraction::getDenominator() const
{
return denominator;
}
//print displays the fractions
void Fraction::print()
{
cout<<numerator<<"/"<<denominator;
}
void Fraction::multiply(Fraction fract1,Fraction fract2)
{
fract1*fract2;
}
I can't seem to see where my problem is. I've searched for the answer and I see a lot of people talk about templates (I think for the same situation), but we haven't covered anything like that. Help would be appreciated. Everything was going fine until this step.
On line 51 you are trying to multiply two fraction objects w/o overloading the * operator for class Fraction first - the error I'm getting is therefore:
error: no match for 'operator*' (operand types are 'Fraction' and 'Fraction') ..
So try overloading operator * first for class Fraction
So, you're not getting an error on line 49? That's where I'm getting my error. It hasn't made it to line 51. I'll read on how to overload operators, since I haven't gotten that far yet.
There are a number of things wrong with your multiply function.
1) You've declared a void function. You want to return the resulting fraction as the value of the function. You don't store the result anywhere.
2) As GunnerFunner mentioned, C++ doesn't know how to multiply a custom class. You don't necessarily have to overlaod the * operator. Your multiply function is usable, but you need to fix your arithmetic.
The conventional C++ way of doing this is to overload the * operator.
1 2 3 4 5 6 7 8
Fraction Fraction::operator * (Fraction fract2) // fract1 is implied (this)
{ Fraction rslt;
rslt.numerator = numerator * fract2.numerator;
rslt.denominator = denominator * fract2.denominator;
// You may want to reduce the result at this point.
return rslt;
}
1) You've declared a void function. You want to return the resulting fraction as the value of the function. You don't store the result anywhere.
Per the instructions, the function needs to remain void, so I guess I'd have to overload the * operator? I was in the middle of doing that, but I see I've made some mistakes.
Actually no, I did it correctly. I coded what you wrote, but do I also need to add rslt.multiply(Fraction fract1,Fraction fract2);? Looking at the example in my textbook, that's what they did.
With or without that statement, I'm still getting the same error for line 49, only this time with the overload function.
Per the instructions, the function needs to remain void
Then how are you supposed to return the result?
but do I also need to add rslt.multiply(Fraction fract1,Fraction fract2);?
You can, but it's not necessary. Either snippet I posted will do the job.
Is this what you're looking for?
1 2 3 4 5 6 7 8 9
// Will place the result in this
void Fraction::multiply (Fraction fract1,Fraction fract2)
{ numerator = fract1.numerator * fract2.numerator;
denominator = fract1.denominator * fract2.denominator;
}
// This would be invoked as
Fraction f1, f2, f3; // Initialize f1, f2 somehow
f3.multiply (f1, f2); // result is placed in f3
I'm still getting the same error for line 49, only this time with the overload function.
I can't duplicate that error with your posted code. I get the same error as GunnerFunner. Are you sure the code you posted matches what you're compiling.
I can't duplicate that error with your posted code. I get the same error as GunnerFunner. Are you sure the code you posted matches what you're compiling.
#include <iostream>
#include "Fraction.h"
usingnamespace std;
int main()
{
Fraction f1;
Fraction f2(1,4);
Fraction f3;
int num, den;
cout<<"Please enter a value for the numerator: ";
cin>>num;
cout<<"Please enter a value for the denominator: ";
cin>>den;
f1.setNumerator(num);
f1.setDenominator(den);
f3.multiply(f1,f2);
cout<<"Fraction 1 is ";
f1.print();
cout<<endl;
cout<<"Fraction 2 is ";
f2.print();
cout<<endl;
cout<<"Fraction 1 times Fraction 2 is ";
f3.print();
return 0;
}
Now, when removing everything that has to do with the multiply function, the program executes.
Line 20: The void function makes sense now. Would have clarified things if you had posted main in your OP. The latest snippet I posted should work for you.
If that snippet doesn't work, please post your current fraction.h and fraction.cpp again please.
#include "Fraction.h"
#include <iostream>
usingnamespace std;
//Constructor initializes the numerator and denominator
Fraction::Fraction()
{
numerator = 0;
denominator = 0;
}
//Constructor accepts arguments for numerator and denominator
Fraction::Fraction(int num,int den)
{
numerator = num;
denominator = den;
}
//setNumerator assigns a value to the numerator member
void Fraction::setNumerator(int n)
{
numerator = n;
}
//setDenominator assigns a value to the denominator member
void Fraction::setDenominator(int d)
{
denominator = d;
}
//getNumerator returns the value in the numerator member
int Fraction::getNumerator() const
{
return numerator;
}
//getDenominator returns the value in the denominator member
int Fraction::getDenominator() const
{
return denominator;
}
//print displays the fractions
void Fraction::print()
{
cout<<numerator<<"/"<<denominator;
}
void Fraction::multiply(Fraction fract1,Fraction fract2)
{
numerator=fract1.numerator*fract2.numerator;
denominator=fract1.denominator*fract2.denominator;
}
Started overloading functions on the next assignment. I understand what's going on, but I don't understand why the output is displaying the wrong sum. I keep getting 4/9 when it should be 19/20. I've set up my overloaded functions based off what was written in the textbook. I get the correct sum for f3, but for f4, I don't.
#include "Fraction.h"
#include <iostream>
usingnamespace std;
//Constructor initializes the numerator and denominator
Fraction::Fraction()
{
numerator = 0;
denominator = 0;
}
//Constructor accepts arguments for numerator and denominator
Fraction::Fraction(int num,int den)
{
numerator = num;
denominator = den;
}
//setNumerator assigns a value to the numerator member
void Fraction::setNumerator(int n)
{
numerator = n;
}
//setDenominator assigns a value to the denominator member
void Fraction::setDenominator(int d)
{
denominator = d;
}
//getNumerator returns the value in the numerator member
int Fraction::getNumerator() const
{
return numerator;
}
//getDenominator returns the value in the denominator member
int Fraction::getDenominator() const
{
return denominator;
}
//print displays the fractions
void Fraction::print()
{
cout<<numerator<<"/"<<denominator;
}
Fraction Fraction::operator*(Fraction fract2) // fract1 is implied (this)
{
Fraction temp;
temp.numerator=numerator*fract2.numerator;
temp.denominator=denominator*fract2.denominator;
// You may want to reduce the result at this point.
return temp;
}
Fraction Fraction::operator+(Fraction fract2)
{
Fraction temp;
temp.numerator=numerator+fract2.numerator;
temp.denominator=denominator+fract2.denominator;
return temp;
}
Are you saying that's what I need to do? That would give the wrong answer as well.
1/5 + 3/4 = (4 + 15) / 9 = 19 / 9, or am I not seeing it correctly?
I need to do (a(d) + c(b)) / (b * d)
I was following the instructions for the assignment and it also shows the exact same code for overloading the + operator in the book. So, will I need to alter it for my needs? It's just not going to be the same every time.
Edit: Yeah, that's what tripped me up. I was thinking the formula was the same no matter what. This worked.