首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++宏有关问题

2013-03-14 
C++宏问题有文件zyl.h,内容如下:#ifndef ZYL_H#define ZYL_H#include iostream#define DEFINE_CLASS_X(c

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中定义
这样还是比较麻烦

引用:
可是,我这个是必须要的啊···

热点排行