学c++的人应该会的

来源:百度知道 编辑:UC知道 时间:2024/06/01 00:22:57
#include<iostream>
using namespace std;
int main()

{int a,b,m;
cin>>a>>b;
int max(int a,int b)
{
int max;
if(a>b)
max=a;
else
max=b;
return (max);
}
m=max(a,b);
cout<<"the max is"<<m<<endl;
system("pause");
return 0;
}
MAX函数在MAIN函数内定义怎么不行啊,

错误出在
error C2601: 'max' : local function definitions are illegal
这个错误代表错误函数定义。。。
自定义函数应当在主函数外定义的。
*编程时候,要注意变量的类型*
全局变量要在外面定义
局部变量要在函数里面定义
把变量类型搞清楚

正确代码如下:
#include<iostream>
using namespace std;
int max(int a,int b)
{
int max;
if(a>b)
max=a;
else
max=b;
return (max);
}
int main()

{int a,b,m;
cin>>a>>b;

m=max(a,b);
cout<<"the max is"<<m<<endl;
system("pause");
return 0;
}

你用的是哪个版本的编译器啊?应该是编译器的原因

你说对了,就是不可以,只能声明

不错~~
函数里面不能定义别的函数~
但是可以申明
#include<iostream>
using namespace std;

int main()
{
int max(int a,int b);
int a,b,m;
cin>>a>>b;

m=max(a,b);
cout<<"the max is"<<m<<endl;
system("pause"