我今天遇到了这个问题,只是想知道如何检查是否在某处定义了一个用typedef定义的新类型.举个例子,我开始使用我从源代码构建的Xerces-c3库并编写了一个xml2text转换器.但我无法在fbsd上找到Xerces-c3端口,因此安装了Xerces-c2库.
当我尝试重新编译我的源代码时,我收到以下错误:
XML2Text.cc:83: error: cannot declare variable 'handler' to be of abstract type 'XML2TextHandlers'
XML2TextHandlers.h:32: note: because the following virtual functions are pure within 'XML2TextHandlers':
/usr/local/include/xercesc/framework/XMLFormatter.hpp:454: note: virtual void xercesc_2_7::XMLFormatTarget::writeChars(const XMLByte*, unsigned int, xercesc_2_7::XMLFormatter*)
Run Code Online (Sandbox Code Playgroud)
我在头文件中使用了以下定义来获取writeChars方法
virtual void writeChars(const XMLByte* const toWrite,
const XMLSize_t count,
XMLFormatter* const formatter );
Run Code Online (Sandbox Code Playgroud)
我检查过XMLSize_t只是通过以下方式声明的unsigned int:
#define XERCES_SIZE_T size_t
typedef XERCES_SIZE_T XMLSize_t;
Run Code Online (Sandbox Code Playgroud)
因此,如果我想使代码兼容两个库,我将如何做?我能想到的一种方法是检查库的版本是否相应定义XMLSize_t.还有其他方法吗?
谢谢,
Shripad
无法直接识别是否定义了typedef.最流行的解决方法是检查定义typedef的文件是否也定义了一个宏.
例如,类型"struct tm"在time.h中定义.如果你查看time.h的副本,顶部会定义一个宏.在VC2010版本中,它是"_INC_TIME",因此您可以编写
#if !defined(_INC_TIME)
// Do whatever
#endif
Run Code Online (Sandbox Code Playgroud)
如果您感兴趣的类型定义了一个宏,那么您可以检查它.