错误:调用"分数::添加(分数&,分数&)"没有匹配函数
Error: No matching function for call to 'fraction::add(fraction&, fraction&)'
[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);
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 我需要将数组传递给函数以平均不同的测试分数
- 如何在函数中将字符串和分数存储为(Int 或 float)
- 如何创建一个具有输出 z 分数的概率输入的函数?(C++)
- void降低();成员函数减少分数
- C++ 使用类和函数的分数计算器
- 它说"没有匹配的构造函数来初始化'分数'
- 只需一个函数调用即可输出分数
- 错误:调用"分数::添加(分数&,分数&)"没有匹配函数
- 如何使用函数找到平均分和总分数的百分比.c++