Nov 16, 2012 at 9:11am
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 Nov 16, 2012 at 9:11am
Nov 16, 2012 at 9:21am
You can't put the function definition inside another function ( main() is a function).
Nov 16, 2012 at 10:31am
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 Nov 16, 2012 at 10:37am