C ++ calling methods, newbie ... compiling errors PLEASE HELP

with the following program, I get a ton of compilation errors when I try to compile it. I think they have something to offer. I am not familiar with C ++ and do not know what causes them. Can anyone please help? Here's 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 compilation 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

0


source to share


1 answer


(Note: Kyle has updated the output since I posted the answer, so some of them are not in proper context.)

First error: you call "GCD", but your function is called "GDC".

Second error: "RationalNumer" is not "RationalNumber".

Most or all of the other bugs will be removed if you fix the second wherever it occurs (and there are five of them - search for "RationalNumer").



New bug in updated release - another typo: there count << "\n";

should be cout << "\n";

.

Two general tips:

  • Read error messages. 8). They often sound like impenetrable jargon or are just lying, but often the answer is there.

  • Only worry about the first mistake. More often than not, a fix that will cause some or all of the others to leave.

+7


source