hello ,evryone ,why I cann't define a function in main()?

Aug 29, 2012 at 6:41am
evrytime,when I define the a function in the main() ,the compile will note: a function definition is not allowed here before "{" token. but,define the function in the public scode is all ok.
Aug 29, 2012 at 7:25am
U cannot declare a function within a function and main() is just another function.

Imagine going to a library to get a book. U tell the librarian what book u want and instead of knowing where the book u want is, she instead asks u to get a reference card from a different book and to bring that reference card back to her. At which point she tells u where that original book u wanted is...why doesn't she just have that reference card with her to save u all that trouble of finding it among all those books?
Last edited on Aug 29, 2012 at 7:26am
Aug 30, 2012 at 2:49am
thank you ,the first sentance can save my problems,but I think the following example is more usefull. unfortunatly, I am a chinese,english is not very good. could you please explain the example for me more ease to understand.
Aug 30, 2012 at 4:14am
Nobody here will be able to give you a clear reason why. C++ simply doesn't allow it.

You can mimic it with lambdas though, if you are using C++11:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
#include <functional>

int main()
{

  static const auto localfunction = [] (int param)
  {
    std::cout << param;
  };

  int foo = 5;
  localfunction( foo );  // prints '5'

}
Aug 30, 2012 at 5:27am
There is no technical problem in allowing closures as far as objects with static storage duration are involved. Objects with an automatic storage duration gives rise to the '"functional argument problem".

The C programming language historically avoids the main difficulty of the funarg problem by not allowing function definitions to be nested

http://en.wikipedia.org/wiki/Funarg_problem

The alternative is for the language to accept that certain use cases will lead to undefined behaviour, as in the proposal for lambda expressions in C++.

http://en.wikipedia.org/wiki/Closure_(computer_science)#Implementation_and_theory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <memory>
#include <functional>

struct base { virtual void foo( int v ) const = 0 ; virtual ~base() {} } ;

std::shared_ptr<base> pb ;

std::function< void(int) > function()
{
    static int s = 10 ;
    int a = 10 ;

    // ok, s has a static storage duration;
    // object of type 'A' can be safely used after 'function' returns
    struct A : base { virtual void foo( int v ) const /*override*/ { s += v ; } } ;
    pb = std::make_shared<A>() ;
    pb->foo(25) ;
    std::cout << s << '\n'  ; // 35

    // C++11: closure; variables in the outer lexical scope are captured by reference
    // will result in undefined behaviour if 'bar' is used outside 'function'
    auto bar = [&] ( int v ) { s += v ; a += v ; } ;
    bar(33) ;
    std::cout << s << ' ' << a << '\n'  ; // 68 43

    // C++11: a is captured by value
    // 'baz' is well behaved even if it is used after 'function' returns
    auto baz = [&s,a] ( int v ) { s +=  v + a ; } ;
    return baz ;
}

Sep 2, 2012 at 12:54am
thanks for all of you !
Topic archived. No new replies allowed.