Visual Studio中的函数声明和函数定义问题
A problem of function declaration and function definition in Visual Studio
我是C++的初学者,遇到了一些问题。如果有人帮我,我会非常感激的。我的英语不是很好。我在Visual Studio中编写代码。当我同时使用重载函数和默认参数时,它可以如下所示:
double max(double a, double b, double c, double d = 3.1415926)
{
cout << "pi: " << d << endl;
if (a > b && a > c)
return a;
else if (b > a && b > c)
return b;
else
return c;
}
int max(int a, int b, int c, int d = 20)
{
cout << "d: " << d << endl;
if (a > b && a > c)
return a;
else if (b > a && b > c)
return b;
else
return c;
}
int main()
{
int x = 10, y = 6, z = 23;
double m = 1.2, n = 4.2, k = 3.1;
cout <<"max value in x, y, z: "<< max(x, y, z) << endl;
cout <<"max value in m, n, k: "<< max(m, n, k) << endl;
return 0;
}
然而,如果我首先声明函数,然后最后定义它,则会出现如下错误:
int max(int a, int b, int c, int d = 20);
double max(double a, double b, double c, double d = 3.1415926);
int main()
{
int x = 10, y = 6, z = 23;
double m = 1.2, n = 4.2, k = 3.1;
cout <<"max value in x, y, z: "<< max(x, y, z) << endl;
cout <<"max value in m, n, k: "<< max(m, n, k) << endl;
return 0;
}
double max(double a, double b, double c, double d = 3.1415926)
{
cout << "pi: " << d << endl;
if (a > b && a > c)
return a;
else if (b > a && b > c)
return b;
else
return c;
}
int max(int a, int b, int c, int d = 20)
{
cout << "d: " << d << endl;
if (a > b && a > c)
return a;
else if (b > a && b > c)
return b;
else
return c;
}
Visual Studio显示严重性代码描述文件行错误C2572"max":重新定义默认参数:参数1
非常感谢。
在函数原型中只指定一次的默认值:
int max(int a, int b, int c, int d = 20);
int main()
{
// code...
}
int max(int a, int b, int c, int d)
{
// code...
}
与double
版本相同
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- Visual Studio中的函数声明和函数定义问题
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 具有enable_if外部类原型的模板类构造函数定义
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为函数定义符号不明确的指针参数
- C++模板专用化 - 无法匹配函数定义
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 找不到 #define 的函数定义
- 根据类型特征更改函数定义?
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 命名空间更改函数定义
- "Type&"与C++函数定义中的"Type*"
- C++:为什么允许在另一个函数中声明函数,而不允许在函数定义中声明?
- 如何从 C++ 中的现有模板函数定义新函数
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 越界成员函数定义是否需要一个完全限定的类名,直到全局范围
- 为什么c++允许成员函数定义中实例的私有成员访问
- Qt基类函数定义
- C++函数定义中参数列表后面额外一对括号的含义