如何访问c++中不同类中定义的variale
我正在使用 xerces 库解析 C++ 中的 xml
,将 xml 元素存储到字符串数组中,我想从
Handler 类的类代码访问此字符串数组 -
#include "MySAX2Handler.hpp"
#include <xercesc/sax2/Attributes.hpp>
#include <iostream>
#include <string>
using namespace std;
const int MAXITEMS = 100;
string resultArray[MAXITEMS];
int cnt = 0;
void MySAX2Handler::startElement(const XMLCh* const uri, const XMLCh* const localname,
const XMLCh* const qname, const Attributes& attrs)
{
char* message = XMLString::transcode(localname);
resultArray[cnt] = message;
cnt++;
for (int idx = 0; idx < attrs.getLength(); idx++)
{
char* attrName = XMLString::transcode(attrs.getLocalName(idx));
char* attrValue = XMLString::transcode(attrs.getValue(idx));
resultArray[cnt] = attrName;
cnt++;
resultArray[cnt] = attrValue;
cnt++;
}
XMLString::release(&message);
}
我想从另一个类访问 resultArray
请帮助我,我是 C++ 新手
I am using xerces library to parse xml in C++
storing xml elements into string array, I want to access this string array from my class
code of Handler class -
#include "MySAX2Handler.hpp"
#include <xercesc/sax2/Attributes.hpp>
#include <iostream>
#include <string>
using namespace std;
const int MAXITEMS = 100;
string resultArray[MAXITEMS];
int cnt = 0;
void MySAX2Handler::startElement(const XMLCh* const uri, const XMLCh* const localname,
const XMLCh* const qname, const Attributes& attrs)
{
char* message = XMLString::transcode(localname);
resultArray[cnt] = message;
cnt++;
for (int idx = 0; idx < attrs.getLength(); idx++)
{
char* attrName = XMLString::transcode(attrs.getLocalName(idx));
char* attrValue = XMLString::transcode(attrs.getValue(idx));
resultArray[cnt] = attrName;
cnt++;
resultArray[cnt] = attrValue;
cnt++;
}
XMLString::release(&message);
}
I want to access resultArray from another class
Please help me I am new to C++
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
resultArray
是一个具有外部链接的全局变量,因此您已经可以从程序中的任何位置访问它。你只需要声明它:resultArray
is a global variable with external linkage, so you can already access it from anywhere in your program. You just have to declare it: