[RESOLVED] Problem with macros for creating get/set functions
"error C2084: function 'D3DXVECTOR3 FName(int)' already has a body"
I am using Visual Studio 2005 professional.
I don't understand why FName (function name) is taken directly as the function name instead of being replaced with the macro argument.
Code:
// Actual syntax
#define Get_InsProp3(FName,PName,PPart1,PPart2,PPart3,DType,DefVal) inline DType FName (int Instance) { Object_Instance* pInstance; if (GameEngine_Object_Instance_IsValid(Instance)) { pInstance = (Object_Instance*)Instance; return DType(pInstance->##PName##.##PPart1, pInstance->PName##.##PPart2, pInstance->PName##.##PPart3); } else { ReportBadReference_Instance(L#FName, Instance); return DType##DefVal; } };ReportBadReference_Instance(L#FName, Instance); return DefVal; } };
// Simplified with multiple lines
#define Get_InsProp3(FName,PName,PPart1,PPart2,PPart3,DType,DefVal)
inline DType FName (int Instance) {
Object_Instance* pInstance;
if (GameEngine_Object_Instance_IsValid(Instance)) {
pInstance = (Object_Instance*)Instance;
return DType(pInstance->##PName##.##PPart1, pInstance->PName##.##PPart2, pInstance->PName##.##PPart3);
} else {
ReportBadReference_Instance(L#FName, Instance); return DType##DefVal;
}
};
Re: Problem with macros for creating get/set functions
Please, look at your post and, please, tell us: can you read/understand this macro definition? :confused:
I can't. :(
Please, edit your post adding Code tags and indentations.
Re: Problem with macros for creating get/set functions
Re: Problem with macros for creating get/set functions
No. The second line is toooooooooo long.
Re: Problem with macros for creating get/set functions
The second line is the same code as below. Just ignore it if you can't read it.
Re: Problem with macros for creating get/set functions
I tested the second variant (with \ added) in VS2005 and it worked like a charm.
Have you tried setting your compiler to pre-process to file?
Re: Problem with macros for creating get/set functions
Pre processing to file gave a lot of empty lines but could be useful for debugging if I know what to search for.
This seems to do the same thing but with the same error.
Code:
#define Get_InsProp3(FName,PName,PPart1,PPart2,PPart3,DType,DefVal)\
inline DType FName (int Instance) {\
Object_Instance* pInstance;\
if (GameEngine_Object_Instance_IsValid(Instance)) {\
pInstance = (Object_Instance*)Instance;\
return DType(pInstance->PName##.##PPart1, pInstance->PName##.##PPart2, pInstance->PName##.##PPart3);\
} else {\
ReportBadReference_Instance(L#FName, Instance);\
return DType##DefVal;\
}\
};
I need something to use with FName to make the precompiler see that it is an argument and not a string.
Re: Problem with macros for creating get/set functions
What, exactly, are you trying to do? Create a macro that creates a function "on the fly"? If so, why not try re-writing the function as a template?
Viggy
Re: Problem with macros for creating get/set functions
Quote:
Originally Posted by
Dawoodoz
Pre processing to file gave a lot of empty lines but could be useful for debugging if I know what to search for.
The idea is, you turn on the 'send preprocessor output to file' option (or whatever it's called), then you can see (instead of wonder or guess) exactly what your '#define Get_InsProp3' macro is expanding to.
If you already did this & still have questions, then post what it expanded to here.
Re: Problem with macros for creating get/set functions
Quote:
Originally Posted by
MrViggy
What, exactly, are you trying to do? Create a macro that creates a function "on the fly"? If so, why not try re-writing the function as a template?
Viggy
I have a lot of get and set functions for my graphics engine and need macros for generating the functions.
This is how one of the final get set interfaces should be in the end.
Code:
D3DXVECTOR3 GameEngine_Object_Instance_GetPosition(int Instance) {
Object_Instance* pInstance;
if (GameEngine_Object_Instance_IsValid(Instance)) { pInstance = (Object_Instance*)Instance;
return D3DXVECTOR3(pInstance->transformation._41, pInstance->transformation._42, pInstance->transformation._43);
} else {
ReportBadReference_Instance(L"GameEngine_Object_Instance_GetPosition", Instance);
return D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}
}
void GameEngine_Object_Instance_SetPosition(int Instance, D3DXVECTOR3 NewPosition) {
Object_Instance* pInstance;
if (GameEngine_Object_Instance_IsValid(Instance)) { pInstance = (Object_Instance*)Instance;
pInstance->transformation._41 = NewPosition.x;
pInstance->transformation._42 = NewPosition.y;
pInstance->transformation._43 = NewPosition.z;
} else {
ReportBadReference_Instance(L"GameEngine_Object_Instance_SetPosition", Instance);
}
}
Re: Problem with macros for creating get/set functions
Of what I understand about function templates, they can only be used for taking multiple datatypes for one function name.
Re: Problem with macros for creating get/set functions
I have found the error. It was a side effect of another macro so that other macros crashed.