C++ 成员构造函数定义不正确
我的程序运行正常,但它告诉我这个函数有错误C++ 成员构造函数定义不正确,c++,function,class,object,constructor,C++,Function,Class,Object,Constructor,我的程序运行正常,但它告诉我这个函数有错误 Fraction(int a, int b) // generate a fraction which is a/b { num = a; if(b > 0) den = b; else den = 1; } 我的程序是为了输出错误而编写的,而不是编译器给我一个错误,所以程序说这个成员构造函数不正确,我需要确保denom永远不会为负。 怎么了 #include <iost
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
#包括
#包括
#包括
使用名称空间std;
类分数
{
公众:
//建造师
分数(整数a,整数b)
//生成一个a/b分数
{
num=a;
如果(b>0)
den=b;
其他的
den=1;
}
分数(整数a)
//生成a/1的分数
{
num=a;
den=1;
}
分数()
//生成一个0/1的分数,即0
{
num=0;
den=1;
}
//成员函数
int get_分子()常数
//返回分数的分子
{
返回num;
}
int get_分母()常量
//返回分数的分母
{
返回巢穴;
}
void reduce()
//把这个分数简化成最简单的形式。例如,
//2/4将减少到1/2
{
num/=gcd();
den/=gcd();
}
分数倒数()常数
//返回这个分数的倒数
{
返回分数(den,num);
}
//好友功能
友元分数运算符+(常数分数和f1,常数分数和f2)
//返回f1和f2之和,
//结果减少了
{
int n=f1.num*f2.den+f2.num*f1.den;
int d=f1.den*f2.den;
分数温度=分数(n,d);
温度降低();
返回温度;
}
友元分数运算符-(常数分数&f1,常数分数&f2)
//返回f1和f2的差值,
//结果减少了
{
int n=f1.num*f2.den-f2.num*f1.den;
int d=f1.den*f2.den;
分数温度=分数(n,d);
温度降低();
返回温度;
}
友元分数运算符*(常数分数&f1,常数分数&f2)
//返回f1和f2的乘积,
//结果减少了
{
int n=f1.num*f2.num;
int d=f1.den*f2.den;
分数温度=分数(n,d);
温度降低();
返回温度;
}
友元分数运算符/(常数分数&f1,常数分数&f2)
//返回f1和f2的商,
//结果减少了
{
int n=f1.num*f2.den;
int d=f1.den*f2.num;
分数温度=分数(n,d);
温度降低();
返回温度;
}
友元分数运算符-(常数分数&f)
//返回f的否定
{
分数温度;
temp.num=-f.num;
返回温度;
}
friend bool运算符<(常数分数和f1,常数分数和f2)
//如果f1小于f2,则返回true。
//否则就错了
{
返回f1.num*f2.den(常数分数和f1,常数分数和f2)
//如果f1大于f2,则返回true。
//否则就错了
{
返回f1.num*f2.den>f2.num*f1.den;
}
友元布尔运算符=f2.num*f1.den;
}
友元布尔运算符==(常数分数和f1,常数分数和f2)
//如果f1等于f2,则返回true。
//否则就错了
{
返回f1.num*f2.den==f2.num*f1.den;
}
朋友布尔接线员!=(常数分数和f1,常数分数和f2)
//如果f1不等于f2,则返回true。
//否则就错了
{
返回f1.num*f2.den!=f2.num*f1.den;
}
friend istream和运算符>>(istream和in、分数和f)
//以a/b的形式输入f,其中b不能为零。也,
//如果b为负,分数将变为正。
//因此,同样,1/-3将更改为-1/3
{
焦炭温度;
在>>f.num>>temp>>f.den中;
如果(f.den<0)
{
f、 num*=-1;
f、 den*=-1;
}
返回;
}
friend ostream&operator对于a=3
和b=-4
您的构造函数将设置num=3
和denom=1
。您需要更正调用方的期望值,或者修复代码以执行以下操作:
if (den > 0){
num = a;
den = b;
}
else{
den = -b;
num = -a
}
对于a=3
和b=-4
,构造函数将设置num=3
和denom=1
。您需要更正调用方的期望值,或者修复代码以执行以下操作:
if (den > 0){
num = a;
den = b;
}
else{
den = -b;
num = -a
}
另外,要添加到上面的解决方案,请处理上面的den==0的情况
所以
另外,要添加到上面的解决方案,请处理上面的den==0的情况
所以
好?你在哪里确保den从不为负?嗯,我不是吗?程序说这个成员构造函数不正确,我需要确保denom从不为负。节目是怎么告诉你的?我们需要更多的细节。特别是负责检测和输出错误的部分//测试两个参数构造函数分数f3(3,-4);//如果(f3.get_分子()!=-3 | f3.get_分母()!=4),则分数应为-3/4{ CUT请添加整个类定义。上面的代码不是有效的C++函数。例如,<代码> Num < /C> >和<代码> DEN >代码>?成员变量?嗯,你在哪里确保<代码> DEN/CONT>永远不是否定的?嗯,不是吗?程序说这个成员构造函数是错误的,我需要确定DE。nom从来都不是负的。程序是如何告诉你的?我们需要更多的细节。特别是负责检测和输出错误的部分//测试两个参数构造函数分数f3(3,-4);//如果(f3.get|numerator()!=-3|f3.get|denominator()!=4),分数应该是-3/4{CUT请添加整个类定义。上述代码不是有效的C++函数。例如,<代码> Num < /C> >和<代码> DEN/CODE>声明的成员变量?您注意到重载操作函数中是否存在其他错误?您注意到重载操作函数中是否存在其他错误?