a function-definition is not allowed here before '{' token

Hi all,
I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

#define func( returnType, name, params, body ) { \
    returnType name params { \
        body; \
    } \
}    

int main( int argc, char* argv[] ) {
    func( int, Add, ( int a, int b ), {
        return a + b;
    } );

    std::cout << Add( 5, 10 ) << std::endl;

    return 0;
}


But it gives the error in the title:
Line 13: error: a function-definition is not allowed here before '{' token
What is wrong?
Thanks!
Last edited on
You can't put the function definition inside another function ( main() is a function).
1. you can't define functions inside another function. you can call them, but not define them there.

2.
1
2
3
4
5
func( int, Add, ( int a, int b ), {
        return a + b;
    } );

    std::cout << Add( 5, 10 ) << std::endl;


you don't define the function "Add" like this

what you want is:
1
2
3
4
int Add(int a, int b)
{
   return a + b;
}
Last edited on
You can't put the function definition inside another function ( main() is a function)

Ah, of course, I hadn't thought of that. Thanks!

@darkmaster: yeah, I know, but I am fascinated by define's and so I'm experimenting with them :)
Topic archived. No new replies allowed.