错误:调用"分数::添加(分数&,分数&)"没有匹配函数

Error: No matching function for call to 'fraction::add(fraction&, fraction&)'

本文关键字:分数 函数 添加 错误 调用      更新时间:2023-10-16
[Error] no matching function for call to 'fraction::add(fraction&, fraction&)'
line 105 which is
f3.add( f1, f2);

这是我尝试编译时收到的错误消息。

实例:我正在尝试创建一个"分数"类,它允许我的教师的预设 int main(( 执行。 到目前为止,我已经构建了一个基本的类,并正在尝试编译以查看它是否有效。

  'My class:
class fraction
{
private:
long num, den; 
public:
void setNum(long i_num)
{
    num=i_num;
}
void setDen(long)
{
}
long getNum()
{
return num; 
}
long getDen()
{
return den;
}
fraction()
{
    num = 1;
    den = 1;
}
fraction(int n, int d)
{
    num = n;
    if (d==0) 
    {
        cout << "Cannot divide by zero" << endl;
        exit(0); // will terminate the program if division by 0 is attempted
    }
    else
        den = d;
}
fraction add(fraction otherFraction)
{
    int n = num*otherFraction.den+otherFraction.num*den;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}
fraction sub(fraction otherFraction)
{
    int n = num*otherFraction.den-otherFraction.num*den;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}
fraction mult(fraction otherFraction)
{
    int n = num*otherFraction.num;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}
fraction div(fraction otherFraction)
{
    int n = num*otherFraction.den;
    int d = den*otherFraction.num;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}

int gcd(int n, int d)
{
    int remainder;
    while (d != 0)
    {
        remainder = n % d;
        n = d;
        d = remainder;
    }
    return n;
}
void print() // Display method
{
    if (den == 1) // e.g. fraction 2/1 will display simply as 2
        cout << num << endl;
    else
        cout << num << "/" << den << endl;
}
};'

我的教练的 int main((:

int main ( )
{ // define seven instances of the class fraction
fraction f1, f2, f3, f4, f5, f6, f7;
//set values for the numerator and denominator to f1 and print 
//them
f1.setDen( 2L);
f1.setNum( 0L);
f1.print();
//set values for the numerator and denominator to f2 and print them
f2.setDen( 4L);
f2.setNum( 3L); 
f2.print();
f3.add( f1, f2);
f3.print();
f4.sub( f1, f2);
f4.print();
f5.mult( f1, f2);
f5.print();
f6.div( f1, f2);
f6.print();
f7.inc(f1);
f7.print(f1);

我的导师告诉我们不要以任何方式编辑main((。

我已经将其追溯到类中的方法

    fraction add(fraction otherFraction)
{
    int n = num*otherFraction.den+otherFraction.num*den;
    int d = den*otherFraction.den;
    return fraction(n/gcd(n,d),d/gcd(n,d));
}

如何在 main(( 中传递变量,以便它们在类中工作?我只学过一种做事方式,这是我的第一堂面向对象课。 他正在教授不同的组织方式,我不理解。 大约一周后,他还没有给我发电子邮件寻求帮助(在线课程(。

任何提示/提示将不胜感激。谢谢。

报告的错误中可以看出,编译器会查找具有以下签名的方法:

fraction::add(fraction&, fraction&)

而您定义的方法具有以下方法:

fraction::add(fraction&)

因此,您缺少一个参数(另一个分数(。我不太确定"add"方法的含义(您的老师称呼它的方式(,但我认为这是关于将两个分数的总和分配给您调用对象的那个,即 f3 = f1 +f2。在这种情况下,您应该像这样实现添加:

void add(const fraction& a,const fraction& b)
{
    int n = a.getNum()*b.getDen()+b.getNum()*a.getDen();
    int d = a.getDen()*b.getDen();
    num = n/gcd(n,d);
    dem = d/gcd(n,d);
}

多或少..:)

PS:我添加了一些"const francion&"作为优化,以避免每次调用函数时都复制参数。这不是绝对必要的,但这是一个非常好的编程实践..;)

比较函数的原型:

fraction add(fraction otherFraction)

按照main的说法:

f3.add( f1, f2);

你看到不匹配了吗?您的函数接受一个参数,但main()使用两个参数调用它。您需要向函数添加一个额外的参数。

我相信f3.add(f1, f2)的预期语义是f3 = f1 + f2

add()是一个接受一个fraction参数的成员函数。但是由于它是在 fraction 的实例上调用的,您将添加两个对象,otherFraction*this*this 是调用 add() 的对象的实例,otherFraction是传递给 add() 的参数:

f1.add(f2);
^       ^
|       |
----    -------------
*this   otherFraction

您需要做的就是将结果分配给f3

f3 = f1.add(f2);