-1

I am making a class Fraction with global functions usage My code looks like below:

#include<iostream>
using namespace std;

ostream & operator<<(ostream & os, Fraction & fr)
{
    return os << fr.get_num() << '/' << fr.get_den();
}

class Fraction
{
private:
    int num, den;
public:
int get_num()
    {
        return num;
    }
    int get_den()
    {
        return den;
    }
};

Main function has call : `cout << f2 << endl;
But i am getting following build erros while compilation:
Error C2805 binary 'operator <<' has too few parameters
fr: undeclared identifier
left of get_num must be struct/union/class

ojas
  • 237
  • 1
  • 4
  • 17

1 Answers1

1

You should change the order of your code like this:

class Fraction
{
private:
    int num, den;
public:
int get_num()
    {
        return num;
    }
    int get_den()
    {
        return den;
    }
};

ostream & operator<<(ostream & os, Fraction & fr)
{
    return os << fr.get_num() << '/' << fr.get_den();
}
stan
  • 133
  • 2