views:

217

answers:

1

I'm trying to run through an example given for the C++ Xerces XML library implementation. I've copied the code exactly, but I'm having trouble compiling it.

error: expected class-name before '{' token

I've looked around for a solution, and I know that this error can be caused by circular includes or not defining a class before it is used, but as you can see from the code, I only have 2 files: MySAXHandler.hpp and MySAXHandler.cpp. However, the MySAXHandler class is derived from HandlerBase, which is included.

MyHandler.hpp

#include <xercesc/sax/HandlerBase.hpp>

class MySAXHandler : public HandlerBase {
public:
    void startElement(const XMLCh* const, AttributeList&);
    void fatalError(const SAXParseException&);
};

MySAXHandler.cpp

#include "MySAXHandler.hpp"
#include <iostream>

using namespace std;

MySAXHandler::MySAXHandler()
{
}

void MySAXHandler::startElement(const XMLCh* const name,
                       AttributeList& attributes)
{
    char* message = XMLString::transcode(name);
    cout << "I saw element: "<< message << endl;
    XMLString::release(&message);
}

void MySAXHandler::fatalError(const SAXParseException& exception)
{
    char* message = XMLString::transcode(exception.getMessage());
    cout << "Fatal Error: " << message
         << " at line: " << exception.getLineNumber()
         << endl;
    XMLString::release(&message);
}

I'm compiling like so:

g++ -L/usr/local/lib -lxerces-c -I/usr/local/include -c MySAXHandler.cpp 

I've looked through the HandlerBase and it is defined, so I don't know why I can't derive a class from it? Do I have to override all the virtual functions in HandlerBase? I'm kinda new to C++.

Thanks in advance.

+1  A: 

Try adding using namespace xercesc; or explicitly specify the namespace for the Xerces classes (e.g. xercesc::HandlerBase).

Edit: There is also the XERCES_CPP_NAMESPACE_USE macro, which should be equivalent to the using statement.

kloffy
The 'using namespace xercesc;' didn't have an impact, but 'xercesc::HandlerBase got rid of the class-name error. Thanks!
aduric
You're welcome. If that worked, the using directive has to work as well, provided that it is placed correctly (that would be after the `#include <xercesc/sax/HandlerBase.hpp>` statement in your case). However, the alternative solution better anyways, because using directives in headers are dangerous. (http://mariusbancila.ro/blog/2009/04/14/avoid-using-directives-in-header-files/)
kloffy