c++ - 错误 : No matching function for call to 'fraction::add(fraction&, fraction&)'

标签 c++ oop object

[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() 中传递变量,以便它们在类中工作? 我只学过一种做事的方法,这是我的第一门面向对象类(class)。他在教不同的组织方式,我不明白。 大约一周(在线类(class)),他还没有给我回邮件寻求帮助。

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

最佳答案

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

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&”作为优化,以避免每次调用函数时都复制参数。这不是绝对必要的,但它是一个很好的编程实践......;)

关于c++ - 错误 : No matching function for call to 'fraction::add(fraction&, fraction&)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25688157/

相关文章:

c++ - Qt Error request for member 'rowCount' in 'a' 非类类型 'QTableWidget*'

c++ - 即使所有项目都已弹出,也需要析构函数来释放内存空间

java - 在JAVA中实例化一个抽象类?

C++ 为什么在一种情况下保存到文件有效而另一种情况无效?

c++ - Boost.MPL 随状态变换?

c++ - 命令提示符中的字符 '<'

java - 从字符串数组的 ArrayList 中删除字符串的可能逻辑?

javascript - 在 TypeScript 中混合到已经实例化的对象

Java:将对象保存在文本文件中?有现成的解决方案吗?

php - 如何从非实例化类中调用对象