3
我有以下代碼:C/C++宏擴展
#define UNIT_BASIC_UNIT_DEF2 (name) UNIT_BASIC_ ## name
#define UNIT_UNIT_TYPE_DEF2 (basic_type, name) UNIT_ ## basic_type ## _ ## name
#define UNIT_BASIC_UNIT_CLASS_NAME2(name) CUnit ## name
#define UNIT_UNIT_TYPE_CLASS_NAME2(basic_type, name) CUnit ## basic_type ## _ ## name
#define UNIT_BASIC_UNIT_DEF (name) UNIT_BASIC_UNIT_DEF2(name)
#define UNIT_UNIT_TYPE_DEF (basic_type, name) UNIT_UNIT_TYPE_DEF2 (basic_type, name)
#define UNIT_BASIC_UNIT_CLASS_NAME(name) UNIT_BASIC_UNIT_CLASS_NAME2(name)
#define UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name) UNIT_UNIT_TYPE_CLASS_NAME2(basic_type, name)
#define UNIT_IMPLEMENT_UNIT_TYPE(basic_type, name) \
CUnitAbstract& UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name)::dup(){\
UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name) * n = new UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name)(this->value);\
return *n;\
}\
CUnitAbstract& UNIT_UNIT_TYPE_CLASS_NAME(basic_type, name) ::operator+(CUnitAbstract& value){\
DYNAMIC_ASSERT(dynamic_cast< UNIT_BASIC_UNIT_CLASS_NAME(basic_type) *>(&value) != NULL);\
CUnitAbstract * tmp = &this->dup();\
*tmp = this->value + conversionTable[UNIT_BASIC_UNIT_DEF(basic_type)][UNIT_UNIT_TYPE_DEF(basic_type, name)] * value.getInBasicUnit();return *tmp;\
}
當我打電話與
UNIT_IMPLEMENT_UNIT_TYPE(DISTANCE, METER)
宏我得到編譯錯誤:
error: 'basic_type' was not declared in this scope
error: 'name' was not declared in this scope
error: expected ']' before 'UNIT_basic_type_name'
error: expected ';' before 'UNIT_basic_type_name'
含義該宏沒有擴展,因爲我想在括號內的最後一行。 我做錯了什麼?
'UNIT_BASIC_ ## name' - >'UNIT_BASIC _ ## name'? – someuser 2014-09-04 06:34:02
你使用什麼編譯器? GCC工作正常,至少給出了不同的錯誤:http://coliru.stacked-crooked.com/a/06956e449b44eee3 – 2014-09-04 06:36:09
我使用g ++(雙重檢查:它是GCC)並更改爲: #define UNIT_BASIC_UNIT_DEF2(name) UNIT_BASIC _ ##名 的#define UNIT_UNIT_TYPE_DEF2(basic_type,名)UNIT _ ## basic_type ## _ ##名 的#define UNIT_BASIC_UNIT_CLASS_NAME2(名稱)CUNIT ##名 的#define UNIT_UNIT_TYPE_CLASS_NAME2(basic_type,名)CUNIT ## basic_type ## _ ## name 沒有幫助。同樣的錯誤。 – 2014-09-04 06:45:54