Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
When I run the code on below, I recieve 'class fraction' has no member named 'inc'
I want to run all functions as friend.
Please advice

What I have tried:

#include <iostream>
#include <string.h>
using namespace std;

class fraction
{
 long num;
 long den;

 public:
 fraction ();
 fraction(long,long);
 fraction(const fraction &);
~fraction();
long getnum (void)const;
long getDen (void)const;
void print  (void);
friend fraction add (const fraction &, const fraction &);
friend fraction sub (const fraction &, const fraction &);
friend fraction mult(const fraction &, const fraction &);
friend fraction div (const fraction &, const fraction &);
friend fraction inc (const fraction &);
};
fraction::fraction(long l_num,long l_den)
{
  num = l_num ;
  den = l_den ;
}
fraction::fraction( )
{
  num = 0L ;
  den = 1L ;
}
fraction::fraction(const fraction& F)
{
    num=F.num;
    den=F.den;
}
fraction:: ~fraction ( )
{
//  cout<<"Destructor"<<endl;
}
long fraction::getDen (void) const
{
  return den ;
}
long fraction::getnum (void) const
{
  return num ;
}
void fraction:: print (void)
{
    if(den==0)
       den=1L;
    cout<<num<<"/"<<den <<endl;
}
fraction add( const fraction &f1,  const fraction &f2)
{
  long num = (f1.getnum ( ) * f2.getDen ( )) + ( f1.getDen ( ) * f2.getnum ( ) );
  long den = (f1.getDen ( ) * f2.getDen ( ));
  return fraction(num, den);
}
fraction sub ( const fraction &f1,  const fraction &f2)
{
  long num = (f1.getnum ( ) * f2.getDen ( )) - ( f1.getDen ( ) * f2.getnum ( ) );
  long den = (f1.getDen ( ) * f2.getDen ( ));
  return fraction(num, den);
}
fraction mult ( const fraction &f1,  const fraction &f2)
{
 long num = (f1.getnum ( ) * f2.getnum ( ));
 long den = (f1.getDen ( ) * f2.getDen ( ));
 return fraction(num, den);
}
fraction div( const fraction &f1,  const fraction &f2)
{
long num = (f1.getnum ( ) * f2.getDen ( ));
long den = (f1.getDen ( ) * f2.getnum ( ));
return fraction(num, den);
}
fraction inc (const fraction &f1)
{
return add(f1,fraction(1,1));
}
int main()
{
fraction f1,f2(5L,0L);
fraction f3(f2);
f1.print();
f2.print();
f3.print();

f3 = add(f3 , fraction (-7,8) );
f1 = add(f2,f3);
f1.print ();

f1 = sub(f2 , f3);
f1.print();

f1 = mult(f2, f3);
f1.print();

f1.inc().inc().print();
f1=div(f2 , f3 );
f1.print();

return 0;
}
Posted
Updated 3-Mar-17 1:25am
Comments
[no name] 3-Mar-17 7:10am    
fraction::inc
Richard MacCutchan 3-Mar-17 7:15am    
Read your code first.

1 solution

You have declared member functions for the operations but did not provide definitions (implementations). Your exiting implementations are global functions and not member functions of the fraction class (the global functions are declared when defined).

Also you are calling inc() without parameter while you only have a inc(const fraction &). So you would have to use inc(f1) instead of f1.inc().

But to concatenate operations they must be performed on the object and change the content. So add the missing member function definition(s) omitting one parameter:
C++
class fraction
{
    // ...
    fraction inc();
};

fraction fraction::inc()
{ 
    *this = inc(*this);
    return *this; 
}
 
Share this answer
 
Comments
CPallini 3-Mar-17 7:44am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900