Error: there is no corresponding function to call 'fraction :: add (fraction &, fraction &)'

[Error] no matching function for call to 'fraction::add(fraction&, fraction&)'
line 105 which is
f3.add( f1, f2);

      

This is the error message I get when I try to compile.

Instance: I'm trying to create a "faction" class that allows my instructor to preset int main () to execute. I've been building the bare-bones class so far and am trying to compile it to see if it works.

  'My class:
class fraction
{
private:
long num, den; 
public:
void setNum(long i_num)
{
    num=i_num;
}
void setDen(long)
{
}
long getNum()
{
return num; 
}
long getDen()
{
return den;
}

fraction()
{
    num = 1;
    den = 1;
}
fraction(int n, int d)
{
    num = n;
    if (d==0) 
    {
        cout << "Cannot divide by zero" << endl;
        exit(0); // will terminate the program if division by 0 is attempted
    }
    else
        den = d;
}

fraction add(fraction otherFraction)
{
    int n = num*otherFraction.den+otherFraction.num*den;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}

fraction sub(fraction otherFraction)
{
    int n = num*otherFraction.den-otherFraction.num*den;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}

fraction mult(fraction otherFraction)
{
    int n = num*otherFraction.num;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}

fraction div(fraction otherFraction)
{
    int n = num*otherFraction.den;
    int d = den*otherFraction.num;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}


int gcd(int n, int d)
{
    int remainder;
    while (d != 0)
    {
        remainder = n % d;
        n = d;
        d = remainder;
    }
    return n;
}
void print() // Display method
{
    if (den == 1) // e.g. fraction 2/1 will display simply as 2
        cout << num << endl;
    else
        cout << num << "/" << den << endl;
}
};'

      

My instructor int main ():

int main ( )
{ // define seven instances of the class fraction
fraction f1, f2, f3, f4, f5, f6, f7;
//set values for the numerator and denominator to f1 and print 
//them
f1.setDen( 2L);
f1.setNum( 0L);
f1.print();

//set values for the numerator and denominator to f2 and print them
f2.setDen( 4L);
f2.setNum( 3L); 
f2.print();
f3.add( f1, f2);
f3.print();
f4.sub( f1, f2);
f4.print();
f5.mult( f1, f2);
f5.print();
f6.div( f1, f2);
f6.print();
f7.inc(f1);
f7.print(f1);

      

My instructor told us not to edit main () in any way.

I traced it back to a method in the class

    fraction add(fraction otherFraction)
{
    int n = num*otherFraction.den+otherFraction.num*den;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}

      

How do I go about passing variables to main () so that they work in the class? I was only taught one way to do things, and this is my first OO class. He teaches different ways of organizing and I don't understand. He hasn't emailed me yet in about a week (online class).

Any hints / advice would be much appreciated. Thank.

+3


source to share


3 answers


as you can see from the reported error, the compiler looks for a method with the following signature:

fraction::add(fraction&, fraction&)

      

and the method you defined has the following:

fraction::add(fraction&)

      



so you are missing an argument (the other part). I'm not entirely sure about the meaning of the "add" method (as your instructor calls it), but I think it should assign the result of the sum of the two fractions to the one you are calling the object on, f3 = f1 + f2. In this case, you must implement the following:

void add(const fraction& a,const fraction& b)
{
    int n = a.getNum()*b.getDen()+b.getNum()*a.getDen();
    int d = a.getDen()*b.getDen();

    num = n/gcd(n,d);
    dem = d/gcd(n,d);
}

      

more or less..:)

PS: I've added a few "const francion &" s as an optimization to avoid copying the arguments every time you call the function. It is not strictly necessary, but it is very good programming practice ..;)

+4


source


Compare your function prototype:

fraction add(fraction otherFraction)

      

as it calls it main

:



f3.add( f1, f2);

      

Do you see a discrepancy? Your function takes one parameter, but main()

calls it with two arguments. You need to add an extra parameter to your function.

I believe the intended semantics for f3.add(f1, f2)

is equal f3 = f1 + f2

.

+1


source


add()

is a member function that takes one parameter fraction

. But since it is called on an instance fraction

, you will add two objects, otherFraction

and *this

. *this

is the instance of the object to be called on add()

, and otherFraction

is the argument passed to add()

:

f1.add(f2);
^       ^
|       |
----    -------------
*this   otherFraction

      

All you have to do is assign the result f3

:

f3 = f1.add(f2);

      

-3


source







All Articles