使用编译错误调用C ++方法

时间:2009-05-03 19:47:52

标签: c++ compilation

使用以下程序当我尝试编译它时,我得到了大量的编译错误。我认为他们有方法调用的东西。我不熟悉c ++,不知道是什么导致了它们。有人可以帮忙吗?这是源代码:

#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;
}

以下是编译错误:

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<<'

1 个答案:

答案 0 :(得分:8)

(注意:自从我发布答案以来,Kyle已经更新了输出,因此其中一些缺少正确的上下文。)

第一个错误:你正在调用“GCD”,但你的功能被称为“GDC”。

第二个错误:“RationalNumer”不是“RationalNumber”。

当你在其发生的所有地方修复第二个错误时,大多数或所有其他错误都会消失(其中有五个 - 搜索“RationalNumer”)。

更新输出中的新错误 - 另一个拼写错误:count << "\n";应为cout << "\n";

两条一般性建议:

  • 读取错误消息。 8-)它们通常看起来是难以理解的行话,或者只是说谎,但通常答案确实在那里。

  • 只担心第一个错误。通常,修复会使其他部分或全部消失。