Defining macro function.

So never used macros much but after seeing some code from verius open source projects i think i need to start using them more.
Anyways i made this macro.

1
2
#define GET_NUM_LENGTH(nNum) { ( sizeof( nNum ) / sizeof( int )) } 
// Get Number of numbers in array 

So this works great if, i use it like this:
1
2
int temp = GET_NUM_LENGTH(nTestArray);
cout << "Size Test: " << temp << endl;

If i try to use it like this:
cout << "Size Test: " << GET_NUM_LENGTH(nTestArray) << endl;
It failes and i get the following errors:

1>AKermanFunc.cpp(50): error C2059: syntax error : '{'
1>AKermanFunc.cpp(50): error C2143: syntax error : missing ';' before '{'
1>AKermanFunc.cpp(50): error C2143: syntax error : missing ';' before '}'
1>AKermanFunc.cpp(50): error C2143: syntax error : missing ';' before '<<'

Do anyone know what am doing wrong here Google is not my friend today.

Cheers
WetCode
Last edited on
Solved:
1
2
3
#define GET_NUM_LENGTH(nNum) \
(sizeof( nNum ) / sizeof( int )) \
// Get Number of numbers in array 


Using it as so:
1
2
3
cout << "Size Test: " << GET_NUM_LENGTH(nLock) << endl;
int temp = GET_NUM_LENGTH(nLock);
cout << "Test2: " << temp << endl;
Last edited on
Topic archived. No new replies allowed.