2015-09-27 84 views
0

好的,我有一个叫做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; 
} 
+2

似乎有一些混淆。你的函数add()需要Fraction并且返回int,但你试图从它返回Fraction对象。不知道你真的想做什么。 – SergeyA

+1

是不是返回值f1 a分数? – Trevor

+0

不符合您提供的签名。 'const int'是返回值。这是没有道理的。您的添加实现也不会添加任何内容。 – drescherjm

回答

3

你叫

Fraction f4 = f1.add(4); 

但你没有任何add方法,它const int作为参数和返回Fraction。你应该简单地实施这种方法。

此外,您发布的add方法返回Fraction,而不是您在标题中指定的int。如果你想要它返回一个int你可以简单地用分母除分子。但是,请注意,如果您有1/2,结果将为0.如果这不符合您的要求,请考虑使用floatdouble而不是int

+0

我也拿出了很多编码,使得这个帖子不那么笨重,但我确实有一个。它仍然给我同样的错误信息,但。 – Trevor

+0

那么在哪一行你会得到那个错误信息呢? – Estiny

+0

const int add(Fraction&f1) { return f1; } – Trevor

0

此方法造成的错误:

const int add(Fraction &f1) 
{ 
    return f1; 
} 

参数类型是分数按引用传递,你就返回一个const int的。

0

Аdd方法应该返回int数。尝试覆盖投射运算符。

相关问题