- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
[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/
I have a question about adding files in git. I have found multiple stackoverflow questions about
我是 visual studio 的新手,来自 Delphi。 我有一个充满 .cs 文件的目录树(根是\Common)。 我还有一个充满应用程序的目录树(根目录是\Applications) 最后,
这个问题在这里已经有了答案: Array's lookup time complexity vs. how it is stored (4 个答案) Time complexity for java
谁能告诉我这两者有什么区别: ALTER TABLE x1 ADD INDEX(a); ALTER TABLE x1 ADD INDEX(b); 和 ALTER TABLE x1 ADD INDEX(
为什么有时我们使用 getChildren() add() 而其他时候我们直接使用 add() es: https://docs.oracle.com/javafx/2/get_started/for
如何使用 bootstrap css 在输入下方添加跨度?我需要做这样的事情: 最佳答案 是这样的吗? http://jsfiddle.net/swm53ran/205/ 您可以使用纯 CSS 来实现
问题 np.add(X, 2*Y, out=X) 比 np.add(X, Y, out=X); np.add(X, Y, out=X).使用 np.add(X, Y, out=X); 是一种实际做法吗
当我跑 git add --intent-to-add .所有未跟踪的文件将其状态从“未跟踪的文件”( git status -s 显示 ?? )更改为“未暂存以进行提交的更改”( git statu
我不知道 .add 之间有什么区别和 .sink.add ? 例子: StreamController myStreamController = StreamController(); stream
getContentPane().add() 和 add() 的意思一样吗? public class TestFrame extends JFrame{ public TestFrame()
git add . 和 git add * 会完成完全相同的事情吗? 最佳答案 不,不会。 * 是一个 glob 模式,不会匹配以 开头的文件。 例如,假设这是当前目录,我有 2 个新文件要添加 fo
git的分支与合并的两种方法 git add -A和 git add . git add -u在功能上看似很相近,但还是存在一点差别 git add . :他会
git add [--all | -A] 之间有什么区别?和 git add . ? 最佳答案 此答案仅适用于 Git 版本 1.x。对于 Git 版本 2.x,请参阅其他答案。 总结: git ad
我刚刚安装了最新的 Wix v3.7。我创建了一个 VS 2010“Excel 2010 加载项”项目,并在同一个解决方案中创建了一个 Wix“安装项目”。 问题是,当我尝试从 Wix 项目中引用 A
YUI.add 和 YUI().add 有什么区别? 最佳答案 在第一种情况下,您要注册一个模块可以加载到 YUI 沙箱中,在第二种情况下,您要构建一个沙箱,然后进行注册(这是一种非常不典型的用法)。
测试代码时,任何输入到列表中的值在按下“enter”后都会消失。 我对编程和网络开发非常陌生。请具体一点,以便我理解。 function addItem(){ var item = documen
我正在浏览 python 的 dis 包。我尝试了代码以查看它是如何工作的 >>> def get(): ... x=4 ... y=x+3 ............ this lin
我已经对我的文件夹进行了版本控制 git init git add . git commit -m 'Initial commit' 我应该怎么做 git add 对于我在 .? 中创建的每个新文件
当我执行 $ git add * 时,有时我意识到 git 不会将已删除的文件添加到舞台上,如果删除或添加它,我需要手动指示,但我想不通找出 $ git add --all 有什么区别。因此,如果星号
这个问题在这里已经有了答案: Difference between "git add -A" and "git add ." (12 个答案) 关闭 6 年前。 目前,当我想提交并将内容推送到远程
我是一名优秀的程序员,十分优秀!