C++ 错误 C2601:'main':本地函数定义非法 - MS VS 2013 编译器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19460076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2601: 'main' : local function definitions are illegall - MS VS 2013 Compiler
提问by enedil
I'm writing a small program in C++. When I try to compile it using MS VS 2013 Compiler I get an error: "C2601: 'main' : local function definitions are illegall". What does it mean? My code is:
我正在用 C++ 编写一个小程序。当我尝试使用 MS VS 2013 Compiler 编译它时,出现错误:“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;
}
}
回答by David Heffernan
Your bracketing is broken. The net result is that you are attempting to define your mainfunction inside ciong. And C++ does not support nested function definitions. Hence the compiler error.
你的包围坏了。最终结果是您试图main在ciong. 而 C++ 不支持嵌套函数定义。因此编译器错误。
The code should be:
代码应该是:
#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;
}
And there are other bugs. For example, you meant cout << ciong(n).
还有其他错误。例如,您的意思是cout << ciong(n).
回答by Michael Haephrati
Using Visual Studio 2013 C++, I got compilation errors that I couldn't explain.
使用 Visual Studio 2013 C++,我遇到了无法解释的编译错误。
The compilation errors were:
编译错误是:
*main.cpp(325): error C2601: 'FLAG' : local function definitions are illegal
*main.cpp(325): 错误 C2601: 'FLAG': 本地函数定义是非法的
main.cpp(323): this line contains a '{' which has not yet been matched
main.cpp(323): 这一行包含一个尚未匹配的“{”
main.cpp(326): fatal error C1075: end of file found before the left brace '{' at 'main.cpp(323)' was matched*
main.cpp(326): 致命错误 C1075: 在匹配 'main.cpp(323)' 处的左大括号 '{' 之前找到文件结尾*
But there was nothing wrong with my code. I counted all brackets and the number matched. There weren't any function inside another function.
但是我的代码没有任何问题。我计算了所有括号和匹配的数字。另一个函数中没有任何函数。
I solved it by removing all "//" comments from the source code. It seems that the reason for that is bad line formatting which causes the compiler to miss a line break, so the line after a comment is treated as a comment as well.
我通过从源代码中删除所有“//”注释来解决它。其原因似乎是错误的行格式导致编译器错过换行符,因此注释之后的行也被视为注释。
For example:
例如:
// This is a comment
This_is_a_line;
is treated as:
被视为:
// This is a comment This_is_a_line;
There are many posts of the net about similar problems and some even suggested that they could be caused by a memory (RAM) fault on the machine, so before you replace your RAM, just remove the comments and see...
网上有很多关于类似问题的帖子,甚至有人提出它们可能是由机器上的内存(RAM)故障引起的,所以在更换内存之前,只需删除评论并查看...
- Michael Haephrati ????? ??????
- 迈克尔·海弗拉蒂?????????

