Xerces C++ SAX 解析问题:预期类名位于 '{' 之前代币
我正在尝试运行为 C++ Xerces XML 库实现提供的示例。我已经准确复制了代码,但编译时遇到问题。
error: expected class-name before '{' token
我四处寻找解决方案,我知道这个错误可能是由于循环包含或在使用之前没有定义类而引起的,但正如您从代码中看到的,我只有 2 个文件:MySAXHandler.hpp 和MySAXHandler.cpp。然而,MySAXHandler 类是从 HandlerBase 派生的,它是包含在内的。
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);
}
我正在像这样编译:
g++ -L/usr/local/lib -lxerces-c -I/usr/local/include -c MySAXHandler.cpp
我已经查看了 HandlerBase 并且它已定义,所以我不知道为什么我不能从中派生类?我是否必须重写 HandlerBase 中的所有虚拟函数?我对 C++ 有点陌生。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加
using namespace xercesc;
或显式指定 Xerces 类的命名空间(例如xercesc::HandlerBase
)。编辑:还有
XERCES_CPP_NAMESPACE_USE
宏,它应该等同于using语句。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.