tags:

views:

113

answers:

2

I faced this problem today and just wondering how to check if a new type defined with typedef is really defined somewhere. To give an example, I started using Xerces-c3 library that I built from source code and wrote a xml2text converter. But I couldnt find Xerces-c3 port on fbsd so installed Xerces-c2 library.

When I tried to recompile my source code I got following error:

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*)

I am using following definition in my header file for writeChars method

virtual void writeChars(const XMLByte* const toWrite,
                        const XMLSize_t count,
                        XMLFormatter* const formatter );

I checked that XMLSize_t is nothing but unsigned int declared with following:

#define XERCES_SIZE_T size_t  
typedef XERCES_SIZE_T XMLSize_t;

So if I want to make a code compatible to both the libraries how will I do it? One way I can think of is to check whether the version of the library and define XMLSize_t accordingly. Any other way?

Thanks,

Shripad

A: 

Not sure about how you would go about checking typedefs, but if theres a macro you can identify in the file with the typedef, you can check define statements with #ifdef

http://gcc.gnu.org/onlinedocs/cpp/Ifdef.html

DrDipshit
Yes I know if its defined with macro I can easily do following#ifndef XMLSize_t#define XMLSize_t unsigned int#endif
Shripad Bodas
A: 

There is no way to directly recognise whether a typedef is defined. The most popular workaround is to check if the file that defines the typedef also defines a macro.

e.g. The type "struct tm" is defined in time.h. If you look at your copy of time.h, there will be a macro defined at the top. In the VC2010 version it is "_INC_TIME" so you can write

#if !defined(_INC_TIME)
    // Do whatever
#endif

If the type you are interested in defines a macro, then you can check for that.

Michael J
Yup, solved the problem by doing following:I found that this type declared in Xerces_autoconf_config.hpp which is introduced in xerces-c3 but its not there in xerces-c2. So did the following:#ifndef XERCES_AUTOCONFIG_CONFIG_HPP#define XMLSize_t unsigned int #endif
Shripad Bodas