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
版本
我是 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
版本