关于默认参数的问题
时间:2010-10-01
来源:互联网
既可以在函数声明也可以在函数定义中指定默认参数
我在声明的时候默认没有错 在定义的时候默认就出错了啊
C/C++ code
#include<iostream> using namespace std; int f(int a,int b); int main() { cout<<f(); } int f(int a=2,int b=3) { return a+b; }
这个定义的默认参数 貌似不管用啊
作者: djh512 发布时间: 2010-10-01
ps:顺便测试一下插入代码
C/C++ code
#include<iostream> using namespace std; int f(int a,int b);
作者: falcomavin 发布时间: 2010-10-01
看c++ primer的时候 它说
既可以在函数声明也可以在函数定义中指定默认参数
我在声明的时候默认没有错 在定义的时候默认就出错了啊
C/C++ code
#include<iostream>
using namespace std;
int f(int a,int b);
int main()
{
cout<<f();
}
int f(in……
作者: ganpengjin1 发布时间: 2010-10-01
作者: djh512 发布时间: 2010-10-01
也就是这个模式才是对的:
C/C++ code
void f(int a, int b=3); int main() { f(2); return 0; } void f(int a, int b/* =3 */) { cout<<b; }
作者: ayw215 发布时间: 2010-10-01
作者: gules 发布时间: 2010-10-01
We can specify default argument(s) in either the function definition or declaration. However, a parameter can have its default argument specified only once in a file. The following is an error:
既可以在函数声明也可以在函数定义中指定默认实参。但是,在一个文件中,只能为一个形参指定默认实参一次。下面的例子是错误的:
// ff.h
int ff(int = 0);
// ff.cc
#include "ff.h"
int ff(int i = 0) { /* ... */ } // error
Default arguments ordinarily should be specified with the declaration for the function and placed in an appropriate header.
通常,应在函数声明中指定默认实参,并将该声明放在合适的头文件中。
If a default argument is provided in the parameter list of a function definition, the default argument is available only for function calls in the source file that contains the function definition.
如果在函数定义的形参表中提供默认实参,那么只有在包含该函数定义的源文件中调用该函数时,默认实参才是有效的。
作者: poiu710 发布时间: 2010-10-01
下面这样就对了。
Untitled1.h
C/C++ code
#ifndef UNT__H #define UNT__H int f(int a,int b); #endif
Untitled1.cpp
C/C++ code
#include <iostream> #include "Untitled1.h" using namespace std; int f(int a=2, int b=3) { cout<< a+b << endl; return 0; } int main() { f(2); system("pause"); return 0; }
作者: poiu710 发布时间: 2010-10-01
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28