【问题标题】:Incorrect member constructor definition成员构造函数定义不正确
【发布时间】:2018-07-30 09:29:23
【问题描述】:

我的程序可以运行,但它告诉我这个函数有错误

Fraction(int a, int b)
// generate a fraction which is a/b
{
        num = a;
    if(b > 0)
        den = b;
    else
        den = 1;  
}

我的程序是为了输出错误而不是编译器给我一个错误而编写的,所以程序说这个成员构造函数不正确,我需要确保 denom 永远不会是负数。 有什么问题?

#include <iostream>
#include <cmath>
#include <cassert>

using namespace std;

class Fraction
{
public:
// constructor

Fraction(int a, int b)
// generate a fraction which is a/b
{
        num = a;
    if(b > 0)
        den = b;
    else
        den = 1;  
}

Fraction(int a)
// generate a fraction which is a/1
{
num=a;
den=1;
}

Fraction()
// generate a fraction which is 0/1. i.e 0
{
num=0;
den=1;
}

// member functions

int get_numerator() const
// return the numerator of the fraction
{
return num;
}

int get_denominator() const
// return the denominator of the fraction
{
    return den;
}

void reduce()
// reduce this fraction to simplest form. For instance,
// 2/4 will be reduced to 1/2
{
    num /= gcd();
    den /= gcd();
}

Fraction reciprocal() const
// return the reciprocal of this Fraction
{
    return Fraction(den, num);
}

// friend functions

friend Fraction operator +(const Fraction& f1, const Fraction& f2)
// return the sum of f1 and f2,
// the result is reduced
{
    int n = f1.num * f2.den + f2.num * f1.den;
    int d = f1.den * f2.den;
    Fraction temp = Fraction(n, d);
    temp.reduce();
    return temp;
}

friend Fraction operator -(const Fraction& f1, const Fraction& f2)
// return the difference of f1 and f2,
// the result is reduced
{
    int n = f1.num *f2.den - f2.num * f1.den;
    int d = f1.den * f2.den;
    Fraction temp = Fraction(n, d);
    temp.reduce();
    return temp;
}

friend Fraction operator *(const Fraction& f1, const Fraction& f2)
// return the product of f1 and f2,
// the result is reduced
{
    int n = f1.num * f2.num;
    int d = f1.den * f2.den;
    Fraction temp = Fraction(n, d);
    temp.reduce();
    return temp;
}

friend Fraction operator /(const Fraction& f1, const Fraction& f2)
// return the quotient of f1 and f2,
// the result is reduced
{
    int n = f1.num * f2.den;
    int d = f1.den * f2.num;
    Fraction temp = Fraction(n, d);
    temp.reduce();
    return temp;
}

friend Fraction operator -(const Fraction& f)
// return the negation of f
{
    Fraction temp;
    temp.num=-f.num;
    return temp;
}

friend bool operator < (const Fraction& f1, const Fraction& f2)
// return true if f1 is less than f2.
// False otherwise
{
    return f1.num*f2.den < f2.num*f1.den;
}

friend bool operator > (const Fraction& f1, const Fraction& f2)
// return true if f1 is greater than f2.
// False otherwise
{
    return f1.num*f2.den > f2.num*f1.den;
}

friend bool operator <= (const Fraction& f1, const Fraction& f2)
// return true if f1 is less or equal to f2.
// False otherwise
{
    return f1.num*f2.den <= f2.num*f1.den;
}

friend bool operator >= (const Fraction& f1, const Fraction& f2)
// return true if f1 is greater or equal to f2.
// False otherwise
{
    return f1.num*f2.den >= f2.num*f1.den;
}

friend bool operator == (const Fraction& f1, const Fraction& f2)
// return true if f1 is equal to f2.
// False otherwise
{
    return f1.num*f2.den == f2.num*f1.den;
}

friend bool operator != (const Fraction& f1, const Fraction& f2)
// return true if f1 is not equal to f2.
// False otherwise
{
    return f1.num*f2.den != f2.num*f1.den;
}

friend istream& operator >> (istream& in, Fraction& f)
// input f in the form of a/b, where b cannot be zero. Also,
// if b is negative, the Fraction will change b to be positive.
// So, again, 1/-3 will be changed to -1/3
{
    char temp;
    in >> f.num >> temp >> f.den;
    if(f.den < 0)
    {
        f.num *= -1;
        f.den *= -1;
    }

    return in; 
 }

friend ostream& operator << (ostream& out, Fraction& f)
// output a Fraction f in form of a/b
{
    out << f.num << " / " << f.den;
    return out; 
}


private:
int num; // numerator of the fraction
int den; // denominator of the fraction

int gcd();
// A prvate function that is used to find the gcd of numerator
// and denominator by using Euclidean Algorithm
};

// all following test functions are given

double test1();
// test all constructors and two get methods.
// All these functions worth 1.5 points

double test2();
// test neg, reduce, and reciprocal functions.
// All these functions worth 1.5 points

double test3();
// test add, sub, mul, and div functions.
// All these functions worth 3 points

double test4();
// test less, greater, equal, less_or_equal, greater_or_equal,
// not_equal. All these functions worth 2 points

 double test5();
// test input and output function. This two functions worth 1 points

【问题讨论】:

  • 嗯?你在哪里确保den 永远不会是负数?
  • 嗯,我不是吗?
  • 程序说这个成员构造函数不正确,我需要确保 denom 永远不会是负数。 程序是怎么告诉你的?我们需要更多细节。特别是负责检测和输出错误的部分。
  • // 测试两个参数构造函数 Fraction f3(3, -4); // 分数应该是 -3/4 if(f3.get_numerator() != -3 || f3.get_denominator() != 4) { cout
  • 请添加整个类定义。上面的代码不是一个有效的 c++ 函数。例如,numden 在哪里声明?成员变量?

标签: c++ function class object constructor


【解决方案1】:

对于a=3b=-4,您的构造函数将设置num=3denom=1。您需要纠正调用方的期望或修复代码:

if (den > 0){
   num = a;
   den = b;
}
else{
   den = -b;
   num = -a
}

【讨论】:

  • 您是否注意到重载的操作函数中还有其他错误?
【解决方案2】:

还要添加到上面的解决方案中,处理上面 den == 0 的情况 所以..

if(den == 0)
{
  den = 1 ;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多