c ++ struct function,返回动态分配并接受struct的新结构

时间:2015-05-06 04:11:06

标签: c++ pointers struct

我正在尝试编写一个返回动态分配的新函数 结构指针,我在返回指针时遇到问题。 如果我只是返回一个结构,我已经得到了这个。但我无法弄清楚如何传递回来。任何帮助都会很棒。

struct fraction
{
    int n;
    int d;
    int gcd = 0;
};

class Worker
{
private:
    fraction * fract = new fraction;

public:

    Worker();
    // prototypes
    fraction* gcd(fraction *);
};

fraction Worker:: *gcd(fraction *f)
{


    int n= f->n;
    int d = f->d;
    int r = 0;
    do
    {
        r = n % d;
        n = d;
        d = r;
    } while (d > 0);
    fract->d = f->d;
    fract->n = f->n;
    fract->gcd = n;

    return fract;

}

1 个答案:

答案 0 :(得分:0)

struct Fraction {
    int numerator;
    int denominator;

    Fraction() {}
    Fraction( int numerator, int denominator ) :
        num( numerator ),
        den( denominator )
    {}
}; // Fraction

class FractionWithGCD {
private:
    Fraction m_fraction;
    int      m_gcd;

public:
    FractionWithGCD(); // Default
    FractionWithGCD( int numerator, int denominator );
    FractionWithGCD( Fraction& fraction );

    int getGCD() const { return m_gcd; }
    Fraction getFraction() const { return m_fraction; }

    void calculateGCD( int numerator, int denominator );
    void calculateGCD( Fraction& fraction );

private:
    void calculate( int numerator, int denominator );

}; // FractionWithGCD

// -------------------------------------------------------------------------
// FractionWithGCD() - Default Constructor
// User needs to use either calculateGCD function they will both create member
// variable fraction and calculate the GCD of that fraction storing the values.
FractionWithGCD::FractionWithGCD() :
m_gcd( 0 ) {
} // FractionWithGCD

// -------------------------------------------------------------------------
// FractionWithGCD() - Takes Two Integers - Will construct a fraction
// variable and calculate the GCD upon construction    
FractionWithGCD::FractionWithGCD( int numerator, int denominator ) :
m_gcd( 0 ) {
    calculate( numerator, denominator );
} // FractionWithGCD

// -------------------------------------------------------------------------
// FractionWithGCD() - Takes a Fraction struct, saves it and calculates the GCD.
FractionWithGCD::FractionWithGCD( Fraction& fraction ) :
m_gcd( 0 ) {
    calculate( fraction.numerator, fraction.denominator );
} // FractionWithGCD

// -------------------------------------------------------------------------
// calculateGCD() - Calls private member calculate() - If this function is called
// again, the current fraction and GCD will be overwritten.
void FractionWithGCD::calculateGCD( int, numerator, int denominator ) {
    calculate( numerator, denominator );
} // calculateGCD

// -------------------------------------------------------------------------
// calculateGCD() - Calls private member calculate() - If this function is called
// again, the current fraction and GCD will be overwritten.
void FractionWithGCD::calculateGCD( Fraction& fraction ) {
    calculate( fraction.numerator, calculate.denominator );
 } // calculateGCD

// -------------------------------------------------------------------------
// calculate() - Private Method - Responsible For Checking If Fraction Is
// Valid That The Denominator Is Not 0, Saved The Fraction To Member Variable
// And Does All The Work To Calculate The GSD And Saves It Into Member Variable
void FractionWithGCD::calculate( int numerator, int denominator ) {
    // Check Denominator For 0 To Prevent Possible Division By 0
    if ( denominator == 0 ) {
        std::cout << "Error: Division By 0." << std::endl;
        return;
    }
    // Save Our Fraction
    m_fraction.numerator = numerator; 
    m_fraction.denominator = denominator;

    // Temp Stack Variables To Work With
    int remainder   = 0;

    do {
        remainder   = numerator % denominator;
        numerator   = denominator;
        denominator = remainder;
    } while ( denominator > 0 );

    m_gcd = numerator;

} // Calculate