tags:

views:

427

answers:

1

with the following program I am getting a ton of compile errors when i try and compile it. I think they have something to to with method calling. I am not familiar with c++ and don't know what is causing them. Can anyone help? Here is the source code:

#include <iostream>
using namespace std;
//getting the greatest common divisor to help reduce fractions
int GDC(int x, int y)
{
    int t;
     while (y!=0)
    {
        t=y;
        y=x%y;
        x=t;
    }
    return x;
}

//constructing a RationalNumber wit a numerator and a denominator as seperate elements.
//it also reduces the fraction as well as show them.
//prevents 0 in the denominator as well as avoid negative denominators
class RationalNumber
{
    public:
        RationalNumber(int num=0, int denom=0)
        {
            numerator=num;
            denominator=denom;
        }
        void show()
        {
            {
                cout << "(" << numerator <<"/";
                cout << denominator << ")";
            }
        }
        void reduce()
        {
            int reduction;
            reduction= GCD(numerator, denominator);
            numerator=numerator/reduction;
            denominator=denominator/reduction;

            if(denominator == 0)
            {
                cout << "Cannot compute with 0 denominator ";
            }
            if(denominator < 0)
            {
                cout << "Cannot compute negative demoninators ";
            }
            else
            {
                //simplifies fraction to a whole number when denominator is 1
                if(denominator == 1)
                {
                    cout << "(" << numerator <<")";
                }
                else
                {
                    cout << "(" << numerator <<"/";
                    cout << denominator << ")";
                }
            }
        }
        RationalNumber operator+(RationalNumber op2);
        RationalNumber operator-(RationalNumber op2);
        RationalNumber operator*(RationalNumber op2);
        RationalNumber operator/(RationalNumber op2);
        RationalNumber operator++();

    private:
        double numerator, denominator;
};

void Accept(int n)
{
    if(n<0)
    {
       cout << "Cannot have negatice denominators: ";
    }
    else if (n == 0)
        cout << "Cannot have zero in the denominator: ";
}

//Division
RationalNumber RationalNumer::operator/(RationalNumber op2)
{
    RationalNumber temp;
    int reduction;
    temp.numerator=(numerator*op2.denominator);
    temp.denominator=denominator*op2.numerator;
    cout << "Division:       ";
    return temp;
}

//Subtraction
RationalNumber RationalNumer::operator-(RationalNumber op2)
{
    RationalNumber temp;
    int reduction;
    temp.numerator=(numerator*op2.denominator)-(op2.numerator*denominator);
    temp.denominator=op2.denominator* denominator;
    cout << "Subtraction:       ";
    return temp;
}

//Multiplication
RationalNumber RationalNumer::operator*(RationalNumber op2)
{
    RationalNumber temp;
    temp.numerator=(op2.numerator*numerator);
    temp.denominator=op2.denominator* denominator;
    cout << "Multiplication:       ";
    return temp;
}

//Addition
RationalNumber RationalNumer::operator+(RationalNumber op2)
{
    RationalNumber temp;
    temp.numerator=(op2.numerator*denominator) + ((numerator)*(op2.denominator));
    temp.denominator=op2.denominator* denominator;
    cout << "Addition:       ";
    return temp;
}

RationalNumber RationalNumer::operator++()
{
    numerator++;
    denominator++;
    return *this;
}


void getFraction(int &x, int &y, int &z, int &t)
{
    cout << "First Fraction: ";
    cout << "\n";
    cout << "numerator: ";
    cin >> x;

    do
    {
        cout << "denominator: ";
        cin >> y;
        Accept(y);
        cout << "\n";
        cout << endl;
    }
    while(y <=0);

    cout << "Second Fraction: ";
    cout << "\n";
    cout << "numerator: ";
    cin >> z;
    do
    {
        cout << "denominator: ";
        cin >> t;
        Accept(t);
        cout << "\n";
        cout << endl;
    }
    while(t <=0);
}

void aftermath(int &x, int &y, int &z, int &t)
{
    RationalNumber obA, obS, obM, obD, ob2(x,y), ob3(z,t);
    char Math;
    cout << "Enter sign (+,-,*,/):  ";
    cin >> Math;
    if(Math == '+')
    {
        obA=ob2+ob3;
        obA.reduce();
        cout << "\n";
    }
    else if(Math == '-')
    {
        obS=ob2-ob3;
        obS.reduce();
        cout << "\n\n";
    }
    else if(Math == '*')
    {
        obM=ob2*ob3;
        obM.reduce();
        cout << "\n\n";
    }
    else if(Math == '/')
    {
        obD=ob2/ob3;
        obD.reduce();
        cout << "\n\n";
    }
    else
        cout << "Invalid sign";
}

int main()
{
    char answer;
    do
    {
        int x,y,z,t = 0;
        getFraction(x,y,z,t);
        RationalNumber obA, obS, obM, obD, ob2(x,y), ob3(z,t);

        ob2.show();
        cout <<" ";
        ob3.show();
        cout << "\n\n";
        aftermath(x,y,z,t);
        cout << "\n\n";
        cout << "Create another fraction? - y or n? ";
        cin >> answer;
        cout << "\n";
    }
    while((answer == 'y'));

    if((answer != 'y'))
    {
        cout << "GOOD-BYE ";
    }
    return 0;
}

and here are the compile errors:

g++ -o Math math.cpp
math.cpp: In member function 'void RationalNumber::reduce()':
math.cpp:37: error: 'GCD' was not declared in this scope
math.cpp: In function 'void getFraction(int&, int&, int&, int&)':
math.cpp:136: error: invalid operands of types '<unresolved overloaded function type>' and 'const char [2]' to binary 'operator<<'
math.cpp:151: error: invalid operands of types '<unresolved overloaded function type>' and 'const char [2]' to binary 'operator<<'

Thanks a lot guys

+7  A: 

(Note: Kyle has updated the output since I posted the answer, so some of this lacks the proper context.)

First error: You're calling "GCD" but your function is called "GDC".

Second error: "RationalNumer" not "RationalNumber".

Most or all of the other errors will go away when you fix the second one in all the places it occurs (and there are five of them - search for "RationalNumer").

New error in updated output - another typo: count << "\n"; should be cout << "\n";.

Two pieces of general advice:

  • Read the error messages. 8-) Often they'll appear to be impenetrable jargon, or be simply lying, but often the answer really is in there.

  • Only ever worry about the first error. More often than not, fixing that will make some or all of the others go away.

RichieHindle
I would say, only ever worry about the easiest-to-fix error. Sometimes a simple thing like a missing semicolon will generate all kinds of other errors, so fix the missing semicolon or whatever FIRST (even if it's not the first error you see) and then recompile and (sometimes) all the other errors will go away too.
MatrixFrog