C++ 在int方法C++;

C++ 在int方法C++;,c++,methods,constructor,C++,Methods,Constructor,好的,我有一个名为分数的构造函数,它以两个整数作为参数,然后我应该有一个名为add()的方法,它应该是一个常量int,它以构造函数分数作为参数,然后返回分数 但是,我一直收到错误消息:“不存在从“分数”到“常量int”的合适转换函数” 在过去的几个小时里,我一直在搜索谷歌,但我似乎找不到任何关于如何绕过这个问题的相关信息。在此方面的任何帮助都将不胜感激,谢谢 #include <iostream> #include <conio.h> #include <sstre

好的,我有一个名为分数的构造函数,它以两个整数作为参数,然后我应该有一个名为add()的方法,它应该是一个常量int,它以构造函数分数作为参数,然后返回分数

但是,我一直收到错误消息:“不存在从“分数”到“常量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;
}
#包括
#包括
#包括
#包括“家庭作业3.h”
使用名称空间std;
//为类声明提供所有缺少的部分
类分数{
公众:
分数(){
}
分数(常数分子,常数分母){
分数::分子=分子;
分数:分母=分母;
}
常量整数相加(分数和f1){
返回f1;
}
字符串getString();
私人:
整数分子=0;
int分母=0;
};
字符串分数::getString(){
//返回分数的字符串。
细流ss;
ss你打电话来

Fraction f4 = f1.add(4);
但是您没有任何
add
方法将
const int
作为参数并返回
Fraction
。您应该简单地实现这种方法


也就是你所发布的<代码>添加<代码>分数>代码>而不是你在页眉中指定的int。如果你想返回一个<代码> int <代码>,你可以简单地用分母来划分分子。但是,注意,如果你有1/2个,结果将是0。如果这不是你想要的,考虑使用<代码>浮点或
double
而不是
int

此方法导致错误:

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

参数类型是通过引用传递的分数,您返回的是常量int。

Аdd方法应返回int数。请尝试重写强制转换运算符。

似乎有些混乱。您的函数add()获取分数并返回int-但您正试图从中返回分数对象。不确定您真正想要做什么。返回值f1不是分数吗?不是您给出的签名。
const int
是返回值。这没有意义。而且您的add实现没有添加任何内容。您应该有2个functions
Fraction&add(int-val)
Fraction&add(const-Fraction&val)
not
const-int-add(france&f1)
哦,好吧,我明白了。出于某种原因,我从来没有想到我可以将该方法声明为一个分数类型。我也做了很多编码,使文章不那么笨重,但我确实有一个。尽管如此,它仍然会给我相同的错误消息。const int add(分数&f1){return f1;}如果
f1
分数&
,为什么它会返回一个
int
?好的,那我怎么把分数变成int值呢?也许用分子除以分母?