我正在用C++编写一个小程序.当我尝试使用MS VS 2013编译器编译它时,我收到一个错误:"C2601:'main':本地函数定义是非法的".这是什么意思?我的代码是:
#include <iostream>
int n;
int pomocniczaLiczba;
using namespace std;
int ciong(int n){
switch (n)
{
case 1:
return 1;
break;
case 2:
return 2;
break;
default:
pomocniczaLiczba = ciong(n - 2) + ciong(n - 1) * ciong(n - 1);
return pomocniczaLiczba;
break;
}
int main()
{
cin >> n;
cout >> ciong(n);
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*nan 15
你的包围被打破了.最终结果是你试图在main里面定义你的功能ciong.C++不支持嵌套函数定义.因此编译错误.
代码应该是:
#include "stdafx.h"
#include <iostream>
using namespace std;
int ciong(int n)
{
switch (n)
{
case 1:
return 1;
break;
case 2:
return 2;
break;
default:
int pomocniczaLiczba = ciong(n - 2) + ciong(n - 1) * ciong(n - 1);
return pomocniczaLiczba;
break;
}
} // <----- Oops, this was missing in your code
int main()
{
int n;
cin >> n;
cout << ciong(n) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
还有其他的错误.例如,你的意思cout << ciong(n).