第十二周项目一(4)
来源:互联网 发布:马克思主义史学 知乎 编辑:程序博客网 时间:2024/05/03 12:29
代码
#include <iostream>using namespace std;const double pi=3.1415926;float area(float r=6.5);//指定r的默认值为6.5float volume(float h,float r=6.5); //指定r的默认值为6.5int main( ){ cout<<area()<<endl; //相当于area(6.5); cout<<area(7.5)<<endl; //形参得到的值为7.5,而不是6.5 cout<<volume(45.6)<<endl; //相当于volume(45.6,6.5) cout<<volume(34.2,10.4)<<endl; //h的值为34.2,r的值为10.4 return 0;}float area(float r){ return pi*r*r;}float volume(float h,float r){ return pi*r*r*h;}
总结:去掉4 行“=6.5”。我的原因是8行调用时出错,r无输入值。
\main.cpp|8|error: too few arguments to function 'float area(float)'|
\main.cpp|4|note: declared here|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 2 seconds) ===|
14行'|改为r=6,5,我以为错在不能赋值。
\main.cpp|4|error: expected ',' or '...' before numeric constant|
\main.cpp||In function 'int main()':|
\main.cpp|8|error: too few arguments to function 'float area(float)'|
\main.cpp|4|note: declared here|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 seconds) ===|
将第5行改为“float h=1,float r”,我以为10行会出错,r无值。
\main.cpp|4|error: expected ',' or '...' before numeric constant|
\main.cpp|5|error: default argument missing for parameter 2 of 'float volume(float, float)'|
\main.cpp||In function 'int main()':|
\main.cpp|8|error: too few arguments to function 'float area(float)'|
\main.cpp|4|note: declared here|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
将第5行改为“float volume(float h=0,float r=6.5)”,不会有影响
总结:通过这个练习,对默认参数的用法及作用有了更深刻的认识
- 第十二周项目一(4)
- 第十二周项目一(4)
- 第十二周项目一
- 第十二周项目一
- 第十二周项目一
- 第十二周项目一
- 第十二周项目一
- 第十二周项目4-银行系统(一)
- 第十二周项目一——阅读程序(4)
- 第十二周 项目一:阅读程序(4)
- 第十二周项目一——程序阅读(4)
- 第十二周项目一阅读程序(4)
- 第十二周项目一 阅读程序(4)默认参数
- 第十二周项目4-银行系统(任务一)
- 第十二周项目一阅读程序(4)
- 第十二周项目一——阅读程序(4)
- 第十二周项目一:阅读程序4
- 第十二周 项目一 程序阅读 4
- SpringMVC-01_SpringMVC环境搭建
- 网络渗透技术入门篇
- Java 中类和方法修饰符
- iOS从相册 相机选择图片
- 彼岸
- 第十二周项目一(4)
- 测试
- 请勿回复
- L~M方法
- 地十二周项目4 银行系统初步1
- [SDUSTOJ 1450]捡石子游戏
- 用jquery ajax做的select菜单,选中的效果
- Java内存区域划分、内存分配原理
- opensuse在宏基笔记本无法调节屏幕亮度---解决方案