好的,我有一个叫做Fractions的构造函数,它将两个整数作为参数,然后我应该有一个名为add()的方法,它应该是一个const int,参数构造函数分数,然后返回分数。在int方法中调用构造函数C++
不过,我不断收到错误消息:分数‘到‘const int的’存在“
在过去的几个小时被冲刷谷歌”从没有合适的转换功能’,但我似乎无法找到任何有关如何绕过这一点。任何帮助,将不胜感激,谢谢!
#include <iostream>
#include <conio.h>
#include <sstream>
#include "homework3.h"
using namespace std;
//Provide all missing parts for the class declarations
class Fraction {
public:
Fraction(){
}
Fraction(const int numerator, const int denominator) {
Fraction::numerator = numerator;
Fraction::denominator = denominator;
}
const int add(Fraction &f1) {
return f1;
}
string getString();
private:
int numerator = 0;
int denominator = 0;
};
string Fraction::getString() {
//Returns a string of the fraction.
stringstream ss;
ss << numerator << "/" << denominator;
return ss.str();
}
int main() {
//Test book problems
Fraction f1(3, 5);
Fraction f2(7, 8);
Fraction f3 = f1.add(f2);
Fraction f4 = f1.add(4);
Fraction f5 = f1 + f2;
cout << f3.getString() << endl; //These should display 59/40
cout << f4.getString() << endl; //These should display 23/5
cout << f5.getString() << endl; //These should display 59/40
f3 = f1.subtract(f2);
f4 = f1.subtract(4);
f5 = f1 - f2;
cout << f3.getString() << endl; //These should display -11/40
cout << f4.getString() << endl; //These should display -17/5
cout << f5.getString() << endl; //These should display -11/40
f3 = f1.multiply(f2);
f4 = f1.multiply(4);
f5 = f1 * f2;
cout << f3.getString() << endl; //These should display 21/40
cout << f4.getString() << endl; //These should display 12/5
cout << f5.getString() << endl; //These should display 21/40
f3 = f1.divide(f2);
f4 = f1.divide(4);
f5 = f1/f2;
cout << f3.getString() << endl; //These should display 24/35
cout << f4.getString() << endl; //These should display 3/20
cout << f5.getString() << endl; //These should display 24/35
//Now for some fun...
f5 = (f1 * f2)/(f3 - f4) + (f5 + f2);
cout << f5.getString() << endl; //These should display 10671000/4200000
cout << "Press any key to continue" << endl;
getch();
return 0;
}
似乎有一些混淆。你的函数add()需要Fraction并且返回int,但你试图从它返回Fraction对象。不知道你真的想做什么。 – SergeyA
是不是返回值f1 a分数? – Trevor
不符合您提供的签名。 'const int'是返回值。这是没有道理的。您的添加实现也不会添加任何内容。 – drescherjm