C++宏问题
有文件zyl.h,内容如下:
#ifndef ZYL_H
#define ZYL_H
#include <iostream>
#define DEFINE_CLASS_X(class_name) \
char NameArray##class_name[]=#class_name;\
class class_name
DEFINE_CLASS_X(ZYL)
{
public:
void Display(){printf("ZYL\n");};
};
#endif
文件zyl.cpp
#include "zyl.h"
文件main.cpp
#include <cstdlib>
#include <iostream>
#include "zyl.h"
using namespace std;
int main(int argc, char *argv[])
{
ZYL* tmp = new ZYL;
system("PAUSE");
return EXIT_SUCCESS;
}
为什么,在zyl.cpp中不包含zyl.h时,就可以直接编译过去,如果包含了zyl.h,
总是报
multiple definition of `NameArrayZYL' ?!!
[解决办法]
前面加上extern char NameArray##class_name[];
然后再到cpp中定义
这样还是比较麻烦