error C2601: 'main' : local function definitions are illegall - MS VS 2013 Compiler

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:

#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;
}
}
3

2 Answers

Your bracketing is broken. The net result is that you are attempting to define your main function inside ciong. And C++ does not support nested function definitions. Hence the compiler error.

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).

Using Visual Studio 2013 C++, I got compilation errors that I couldn't explain.

The compilation errors were:

*main.cpp(325): error C2601: 'FLAG' : local function definitions are illegal

main.cpp(323): this line contains a '{' which has not yet been matched

main.cpp(326): fatal error C1075: end of file found before the left brace '{' at 'main.cpp(323)' was matched*

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...

  • Michael Haephrati מיכאל האפרתי
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like