编写程序过程中遇到重定义默认参数的错误,如下例所示:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Student
{
private:
int number;
char name[10];
public:
Student(int n = 0, char *s = "no name");
};
Student::Student(int n = 0, char *s = "no name")
{
number = n;
strcpy_s(name, s);
}
int main()
{
Student s1;
return 0;
}
请教他人发现了原因所在:
1:参数的默认值只可以出现在函数声明中,不可以出现在函数的定义中,否则会出现参数重复定义默认参数的错误---->>语法规定
1定义和声明分开:默认值只可以出现在声明中
2定义和声明不分开,默认值只能出现在定义中
例外:(vs2013可以将默认值出现在定义而不出现在声明中)---->>不把标准当回事
如下所示:
经测试,在vc6.0以及Liunx平台下,当定义和声明分开时:默认值只可以出现在声明中
建议编程者还是按标准做得好