0

In the function for overloading the >> operator I am getting this error:
84`rationalize_number' undeclared (first use this function)

this is the function:

istream& operator >>(istream& in, Rational& rational1)
{
char tempChar;
int numerator = rational1.numerator;
int denominator = rational1.denominator;
       in >> tempChar;
       if(tempChar == '(')
       {     
           in >> rational1.numerator
              >> tempChar;
       }
       if(tempChar == ',')
       {
           in >> rational1.denominator
              >> tempChar;
       }
       rationalize_number(numerator,denominator);
       cout << rational1;
}

Here is the whole program:

#include <iostream>
#include <cstdlib>
using namespace std;



class Rational
{
public:
       Rational();
       Rational(int numerator1);
       Rational(int numerator1, int denominator1);
       friend Rational operator -(Rational rational1, Rational rational2);
       friend Rational operator +(Rational rational1, Rational rational2);
       friend Rational operator /(Rational rational1, Rational rational2);
       friend Rational operator *(Rational rational1, Rational rational2);
       friend bool operator <(Rational rational1, Rational rational2);
       friend bool operator >(Rational rational1, Rational rational2);
       friend bool operator ==(Rational rational1, Rational rational2);
       friend bool operator >=(Rational rational1, Rational rational2);
       friend ostream& operator <<(ostream& out,Rational& rational1);
       friend istream& operator >>(istream& in, Rational& rational1);

private:
        int numerator;
        int denominator;
        void rationalize_number(int& numerator, int& denominator);
};

int main()
{
    Rational test1(24,6), test2(24);
    Rational test4;
    cout << test1<< endl;
    cout << test2<< endl;
    cout << test4<<endl;

    cout << "testing input function" << endl;
    cin >> test4;
    cout << endl;
    cout << test4 << endl;
    system("pause");
}

Rational::Rational() : numerator(0), denominator(1)
{ 
    //empty body
}

Rational::Rational(int numerator1)
{
    numerator = abs(numerator1);
    denominator = 1;
} 

Rational::Rational(int numerator1, int denominator1)
{
    rationalize_number(numerator1,denominator1);                  
    numerator = numerator1;
    denominator = denominator1;

}

ostream& operator <<(ostream& out,Rational& rational1)
{
       out << rational1.numerator <<"/"<<rational1.denominator;
       return out;
}

istream& operator >>(istream& in, Rational& rational1)
{
char tempChar;
int numerator = rational1.numerator;
int denominator = rational1.denominator;
       in >> tempChar;
       if(tempChar == '(')
       {     
           in >> rational1.numerator
              >> tempChar;
       }
       if(tempChar == ',')
       {
           in >> rational1.denominator
              >> tempChar;
       }
       rationalize_number(numerator,denominator);
       cout << rational1;
}







void Rational::rationalize_number(int& numerator, int& denominator)
{
int countDenom;
int countNum;
    //cout << numerator<< "/" << denominator << endl;
    if(numerator >= denominator)
    {
        for (countDenom = denominator; countDenom >= 2; countDenom--)
        {   //cout << "in for" <<numerator<< "/" << denominator << endl;
            //cout << "loop #" << countDenom << endl;
            if(denominator%countDenom == 0 && numerator%countDenom == 0)
            {
                //cout << "if:"<<endl;
                //cout << numerator<< "/" << denominator << endl;                      
                denominator = denominator/countDenom;
                numerator = numerator/countDenom;
            }
            //else
            //{
            //    ;
            //}
        }
    }
    else if(denominator > numerator)
    {
        for (countNum = numerator; countNum >= 2; countNum--)
        {
            if(denominator%countNum == 0 && numerator%countNum == 0)
            {
                denominator = denominator/countNum;
                numerator = numerator/countNum;
            }
            //else
            //{
            //    ;
            //}
        }
    }   
}*
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by rajenpandit
0

Hi szejna,

As the error says 'rationalize_number' is undeclared in this
"istream& operator >>(istream& in, Rational& rational1)" function, you should have check the scope of 'rationalize_number'. you are directly calling this function but as per your code it is private. but still you can use the private member of an object from the function which is declared as friend inside the class.
you are getting error because you are directly calling the funtion, you need to call by using an object as you are accessing private member rational1.denominator. like that you can call the funtion rational1.rationalize_number(numerator,denominator);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.