문제

Anybody please elaborate these error:-

void main()
{
    int a=5, b=60, func();

    printf("\nI am in main-1");

    int func(){
        printf("\nI am in funct");
        return 1;
    }
    func();
    printf("\nI am in main-2");
}

The errors I get are:

  • In function 'main':
  • Line 8: error: static declaration of 'func' follows non-static declaration
  • Line 4: error: previous declaration of 'func' was here
  • Line 3: warning: return type of 'main' is not 'int'

I think C allows nested class because the following code is working fine:

void outerfunc()
{
    int func()
    {
        printf("\nI am in funct");
        return 1;
    }

    func();
}

void main()
{
    printf("\nI am in main-1");

    outerfunc();
    printf("\nI am in main-2");
}
도움이 되었습니까?

해결책

You are using an extension of the GNU C Compiler which allows the declarations of nested functions. The error comes from the fact, that forward declarations of nested functions under GCC's extension need to be prepended with the auto keyword.

int a=20,b=11;
int main()
{
  int a=5, b=60; 
  auto int func(); // <--------- here
  func(); // <- call it
  printf("\nI am in main-1");

  int func(){
  printf("\nI am in funct");
  return 1;
  }

printf("\nI am in main-2");
 return 0;
}

See http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html for more details.

다른 팁

ANSI C doesn't allow nested function definition. And your main function should return int.

Nested functions are not allowed in standard C/C++. Simply (forward) declare the func() inside main() if you want to define it later on.

int main()
{
  int a=5, b=60, func();

printf("\nI am in main-1");

  int func();  // <---- declare inside main()

printf("\nI am in main-2");
}


int func(){    // <---- define later
  printf("\nI am in funct");
  return 1;
}

What you are taking about is a GCC specific feature, its never been a "proper" C feature (i.e. part of the ANSI C specification).

If you want to use this feature then I believe what you are after is this:

#include <stdio.h>

int a = 20, b = 11;

int main( int argc, char* argv[] )
{
    int a = 5, b = 60;
    auto int func( void );

    printf("\nI am in main-1");

    int func( void )
    {
        printf("\nI am in funct");
        return 1;
    }

    printf("\nI am in main-2");
    return func();
}

The reason why your previous code didn't work is because nested functions have no linkage:

A nested function always has no linkage. Declaring one with extern or static is erroneous. If you need to declare the nested function before its definition, use auto (which is otherwise meaningless for function declarations).

The above sample uses the auto keyword thusly. I've also taken the liberty of fixing your main declaration :-)

Nested functions are a gcc-specific extension; they are not universally supported.

As far as the warning about main, the standard signatures for main are

int main(void)
int main(int argc, char **argv) // or equivalent

An implementation may provide additional signatures (some compilers allow a third parameter for environment variables), but those additional signatures must be documented by the implementation; IOW, void main() is only a valid signature for main if your compiler documentation explicitly lists it as such.

When in doubt, use one of the standard signatures above.

  1. You haven't defined func before calling it.
  2. Relates back to the original line.
  3. You aren't returning int.

It works if you remove the declaration of func() in the int variable declarations.

C++ does not allow functions to be included inside other functions.

Attempting to do so in VS 2010 gives:

'funct' : local function definitions are illegal

You need to move that function and it's declaration outside of main.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top