得到错误,但不明白为什么

时间:2014-10-30 05:58:50

标签: c++

我有一个名为Fraction的类,我试图在屏幕上打印之前减少分数。我有一个名为redue()的函数试图这样做,但不知怎的,这不起作用。这就是我所拥有的:

// Fraction.cpp 
// Implemenation for Fraction header file
//

#include <iostream>
#include <iomanip>
#include "Fraction.h"
#include <stdexcept> 
using namespace std;


// Constructor: empty
Fraction:: Fraction(void) {}

// Construcotr: Intializing numerator and denomenator
Fraction:: Fraction( int a, int b)
{
    if (b == 0)
    {
        throw invalid_argument("Error: division by zero");
    }

    if(b != 0)
    {
         reduce(a,b);
         setNumo(a);
         setDeno(b);
    }
}

// Overloading operators
ostream &operator<<(ostream &output, const Fraction &fraction)
{
    if( fraction.numo == fraction.deno)
     {   output<<1;
         return output; }

    if (fraction.deno == 1)
    { output <<fraction.numo;
      return output;}

    if((fraction.numo >=0) && (fraction.deno > 0))
    {
         output <<fraction.numo <<"/"<<fraction.deno;
         return output; // prints a/b 
    }

}

// insertion operator overloading
istream &operator>>( istream &input, Fraction &fraction)
{
    int n1,n2;
    input >> n1;
    input >> fraction.ch;
    input >> n2;

    Fraction temp(n1, n2);
    fraction.numo = temp.numo;
    fraction.deno = temp.deno;
    return input;
}

// = operator
const Fraction& Fraction::operator=(const Fraction &f)
{
    numo = f.numo;
    deno = f.deno;
    return *this;
}

Fraction Fraction::operator-(const Fraction &fraction) const {
    Fraction temp;
    int numo1, numo2;
    temp.numo = numo * fraction.deno - fraction.numo * deno;
    temp.deno = deno * fraction.deno;
    Fraction::reduce(temp.numo, temp.deno);
    return temp;
}

Fraction Fraction::operator+(const Fraction &fraction) const {
    Fraction temp;
    temp.numo = numo * fraction.deno + fraction.numo * deno;
    temp.deno = deno * fraction.deno;
    Fraction::reduce(temp.numo, temp.deno);
    return temp;
}







void Fraction:: setNumo(int a)
{  numo = a; }

void Fraction:: setDeno(int b)
{  deno = b; }


static void Fraction::reduce(int &num1, int &num2)
{
    int gcd;
    gcd = find_gcd(num1, num2);

    if(num2 != 0)
    {
        num1 = num1/gcd;
        num2 = num2/gcd;
    }
}



int Fraction::find_gcd(int num1, int num2) // returns gcd of two numbers
{
    int gcd, remainder;
    remainder = num1 % num2;
    while( remainder !=0)
    {
        num1 = num2;
        num2 = remainder;
        remainder = num1 % num2;
    }
    gcd = num2;
    return gcd;
}

以下是我尝试编译此文件时出现的错误:

Fraction.cpp:102:50: error: cannot declare member function ‘static void Fraction::reduce(int&, int&)’ to have static linkage [-fpermissive]
 static void Fraction::reduce(int &num1, int &num2)
                                                  ^
Fraction.cpp: In static member function ‘static void Fraction::reduce(int&, int&)’:
Fraction.cpp:105:30: error: cannot call member function ‘int Fraction::find_gcd(int, int)’ without object
     gcd = find_gcd(num1, num2);

有人可以告诉我问题是什么以及如何解决这个问题?我真的需要帮助。先感谢您。

2 个答案:

答案 0 :(得分:0)

static void Fraction::reduce(int &num1, int &num2)
{
    int gcd;
    gcd = find_gcd(num1, num2);   <<<<<<<<<<This is a member function

    if(num2 != 0)
    {
        num1 = num1/gcd;
        num2 = num2/gcd;
    }
}

当你声明函数为静态时,编译器不会在该函数调用中插入这个指针,这意味着你不能直接在某个静态函数中调用任何其他成员函数,也不能使用类的数据成员。

答案 1 :(得分:0)

我认为您的两条错误消息非常明确:

Fraction.cpp:102:50: error: cannot declare member function ‘static void Fraction::reduce(int&, int&)’ to have static linkage [-fpermissive]
 static void Fraction::reduce(int &num1, int &num2)

这意味着您已将Fraction::reduce(int, int)声明为成员函数(可能是在Fraction.h中),但现在当您正在实施它时,您会尝试将其设置为静态。删除static,这没关系。

Fraction.cpp: In static member function ‘static void Fraction::reduce(int&, int&)’:
Fraction.cpp:105:30: error: cannot call member function ‘int Fraction::find_gcd(int, int)’ without object
     gcd = find_gcd(num1, num2);

这是从第一个错误开始的。 C ++中的static成员函数是类型的函数,而不是该类型的实例。您已reduce静态但find_gcd非静态。所以reduce作为没有this指针的类函数执行;当它调用find_gcd时,它不知道Fraction的哪个实例用作this指针。使reduce非静态也会解决这个问题。