So for my beginning c++ programming class we are writing a program that creates a class for fractions then performing arithmetic on the objects created. The assignment gives a definition for the class Fraction, and here are the public members which I'm having the most trouble with understanding. The objects created are f1, f2, and fresult.
public:
void inputFrac();
void printFrac();
Fraction fracMult(Fraction b);
Fraction fracDiv(Fraction b);
Fraction fracAdd(Fraction b);
Fraction fracSub(Fraction b);
I get that by creating a class you are creating a type, but when I make a call in the main for the fracMult function exactly like was also in the template provided to us for the program-->
fresult = f1.fracMult(f2);
and my function outside of main looks like this
Fraction :: Fraction fracMult(Fraction b)
{
Fraction x;
x.numerator = fraction.numerator * b.numerator;
x.denom = fraction.denom * b.denom;
if (fraction.positive == true && b.positive == false ||
fraction.positive == false && b.positive == true)
{
x.positive = false;
}
else (fraction.positive == true && b.positive == true ||
fraction.positive == false && b.positive == false)
{
x.positive = true;
}
return (x);
}
I get an error
main.cpp:79:1: error: 'Fraction::Fraction' names the constructor, not the type Fraction :: Fraction fracMult(Fraction b)
I'm just really confused in general about how the type for the functions in the class can be the name of the class, and how I'm supposed to then format the function outside of main. Any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire