[英]Can't use macro define class in C++
I want to generate many sub-class which only has little difference, so I want to use macro to simplify my job. 我想生成许多只具有很小差异的子类,因此我想使用宏简化我的工作。 The macro define below:
宏定义如下:
#define DECLARE_SUB_CLASS(sub_class_name, base_class_name, value1) \
class sub_class_name:base_class_name \
{ \
public: \
virtual int initialize(const void *); \
virtual int run(const void *); \
virtual void reset(); \
virtual int output(const char*); \
virtual void terminate(); \
private: \
static const char m_szValue=#value1; \
};
I use it like this: 我这样使用它:
DECLARE_SUB_CLASS(RTCount13, RTCountBase, 13);
when I compiling with VC2005, it say 当我用VC2005编译时,它说
error C2065: 'RTCount13' : undeclared identifier
what's the problem? 有什么问题?
Use gcc -E (or similar for other preprocessor) 使用gcc -E(或其他预处理器类似)
gcc -E prepro.cxx gcc -E prepro.cxx
# 1 "prepro.cxx"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "prepro.cxx"
# 17 "prepro.cxx"
class RTCount13:RTCountBase {
public:
virtual int initialize(const void *);
virtual int run(const void *);
virtual void reset();
virtual int output(const char*);
virtual void terminate();
private:
static const char m_szValue="13";
};;
You try to assign "13"
to a char. 您尝试将
"13"
分配给一个字符。
By the way you could also use a template instead of a macro to do the exact same thing your macro did (namely to declare but not define the methods). 顺便说一句,您也可以使用模板而不是宏来执行与宏完全相同的操作(即声明但不定义方法)。 Here's a complete (trimmed down) example with separate method definitions.
这是一个完整的(经过精简的)示例,其中包含单独的方法定义。
#include <iostream>
class RTCountBase {};
template <class base_class_name, int v>
class RTCount: base_class_name {
public:
virtual int output();
virtual void terminate();
private:
static const int m_szValue=v;
};
template <class base_class_name, int v>
int RTCount<base_class_name,v>::output(){ return m_szValue; }
template <class base_class_name, int v>
void RTCount<base_class_name,v>::terminate(){ std::cout <<" term "<<std::endl; }
typedef RTCount<RTCountBase,13> RTCount13; // typedef instead of macro
typedef RTCount<RTCountBase,14> RTCount14;
int main(){
RTCount13 myc13;
RTCount14 myc14;
std::cout << "my13: "<<myc13.output()<<std::endl;
std::cout << "my14: "<<myc14.output()<<std::endl;
return 0;
}
Since you're using C++, you really should avoid using #define
to do this. 由于您使用的是C ++,因此您实际上应该避免使用
#define
来执行此操作。 The C++ language has better ways to do this, for example: C ++语言具有更好的方法来执行此操作,例如:
template <class B, const char C>
class SomeClass : public B
{
public:
virtual int initialize(const void *) {return m_szValue;}
virtual int run(const void *) {return 0;}
virtual void reset() {}
virtual int output(const char*) {return 0;}
virtual void terminate() {}
private:
static const char m_szValue=C;
};
and then change this: 然后更改此:
DECLARE_SUB_CLASS(RTCount13, RTCountBase, 13);
RTCount13 some_instance;
to: 至:
SomeClass <RTCountBase, 13> some_instance;
You're missing a hash sign when defining your static char: 定义静态字符时,您缺少哈希符号:
static const char m_szValue=#value1;
instead of 代替
static const char m_szValue=##value1;
EDIT: 编辑:
If m_szValue is meant to be a string
, you need to define it outside the class declaration. 如果m_szValue是
string
,则需要在类声明之外定义它。 I suggest you define a macro for this also to keep consistency: 我建议您为此定义一个宏以保持一致性:
#define DECLARE_SUB_CLASS(sub_class_name, base_class_name) \
class sub_class_name:base_class_name \
{ \
//.....\
private: \
static const char* m_szValue; \
};
#define DEFINE_SUB_CLASS_STATIC(sub_class_name, value1) \
const char* sub_class_name::m_szValue = #value1; \
which you use like this: 您可以这样使用:
//header file
DECLARE_SUB_CLASS(RTCount13, RTCountBase);
//impl file:
DEFINE_SUB_CLASS_STATIC(RTCount13, 13);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.