Defining macro function.
Dec 27, 2013 at 5:16am UTC
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 Dec 27, 2013 at 5:43am UTC
Dec 27, 2013 at 5:42am UTC
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 Dec 27, 2013 at 5:45am UTC
Topic archived. No new replies allowed.