【问题标题】:Error: No matching function for call to 'fraction::add(fraction&, fraction&)'错误:没有匹配函数调用'fraction::add(fraction&, fraction&)'
【发布时间】:2014-10-30 13:00:09
【问题描述】:
[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(const fraction&amp;, const fraction&amp;),并为其添加实现。

标签: c++ oop object


【解决方案1】:

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

fraction::add(fraction&, fraction&)

而您定义的方法有以下一种:

fraction::add(fraction&)

所以,您缺少一个参数(另一个分数)。 我不太确定“添加”方法的含义(您的讲师调用它的方式),但我认为这是关于将两个分数之和的结果分配给您调用对象的那个,即是 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&”作为优化以避免每次调用函数时复制参数。这不是绝对必要的,但它是一种非常好的编程习惯..;)

【讨论】:

  • 你速度更快,有一个 +1 :-)
  • 感谢快速准确的回复。你的建议对解决我遇到的一系列问题非常有帮助。但是,我确实必须从参数中删除“const”,因为某种原因它不允许我拥有它们。现在我只需要让'打印'功能工作,我很高兴。非常感谢。
  • @TonisWhite 它不允许它们的原因是你的getter函数没有标记const(但它们应该是:long getNum() const { return num; }。这表示“这个函数不能修改对象它被调用”,因此也可以在 const 对象(或通过 const &amp; 保存的对象)上调用。
  • const &amp; 并没有真正为你带来像这样的小值类型。在这里没关系,因为函数简单且内联,但在其他地方const &amp; 对小值类型可能是悲观的。特别是,它可以强制编译器发出不必要的内存访问以满足别名规则。
  • @StuartOlsen,是的,你是正确的第一部分。但即使他们没有给你任何东西,在这种特殊情况下,我认为向初学者展示一种在绝大多数情况下都是“好”的良好做法会很有用......但我错过了第二个一个(关于悲观的),你能把它扩大一点吗?
【解决方案2】:

比较你的函数原型:

fraction add(fraction otherFraction)

main 的称呼方式:

f3.add( f1, f2);

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

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

【讨论】:

  • 感谢您的提示。我现在看到我的论点是错误的,需要对类实例进行第二次引用。谢谢安吉。
【解决方案3】:

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

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

您只需将结果分配给f3

f3 = f1.add(f2);

【讨论】:

  • -1,OP明确声明他们不允许更改main()的内容。
  • 感谢您的帮助,信息已被吸收和吸收。但是,Angew 是正确的,因为我的讲师严格禁止更改他的 int main()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
相关资源
最近更新 更多