如何在迭代器c++上调用toString
How to call toString on iterator c++
你好,我是编码新手,很抱歉我可能有任何误解,以及它看起来有多糟糕。我花了几个小时在这个问题上,但无法解决它。我有一个XMLItems向量和一个常量toString方法。当我尝试用迭代器调用toString时,它什么也不输出。
string XMLParser::toStringInput() const
{
string inputret = "";
for(std::vector<XMLItem>::const_iterator iter = theInput.begin(); iter != theInput.end(); ++iter)
{
inputret += (*iter).toString();
}
return inputret;
}
这不会返回任何结果。我用错迭代程序了吗?创建矢量时,字符串是否保存错误?这是XMLItem类中的toString
string XMLItem::toString() const
{
cout << this->theItem; //the item is a private string
return this->theItem;
}
这是我创建向量的地方,以防万一。
void XMLParser::readXML(Scanner& inStream)
{
string tmp = "";
string tag = "tag";
string data = "data";
XMLItem localxml = XMLItem();
while (inStream.hasNext())
{
string input = inStream.nextLine();
if(input.find("<") != std::string::npos)
{
XMLItem localxml = XMLItem(tag, input);
}
else
{
XMLItem localxml = XMLItem(data, input);
}
this->theInput.push_back(localxml);
}
}
XMLItem localxml = XMLItem();
while (inStream.hasNext()) {
string input = inStream.nextLine();
if(input.find("<") != std::string::npos) {
XMLItem localxml = XMLItem(tag, input);
} else {
XMLItem localxml = XMLItem(data, input);
}
this->theInput.push_back(localxml);
}
在if
的块和else
的块中,都有一个名为localxml
的新本地(对应于相应块(变量。它们对while
循环之前定义的变量进行阴影处理,使其保持不变。所以你基本上可以运行
theInput.push_back(XMLItem());
在那个循环中。因此,稍后,当您尝试将向量的元素转换为字符串时,这些"空"元素会被转换,可能会导致一些空字符串被连接起来。
要解决此问题,请删除变量名称前面的类型,将变量声明更改为赋值:
localxml = XMLItem(tag, input);
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- luabind:不能调用基本的lua函数,如print,tostring。
- 如何在迭代器c++上调用toString