在此循环中,我toString()
对svg标签的孩子调用了方法。但是每次我在第一次迭代中遇到细分错误时。
std::list<Tag*> children;
std::string TagSvg::toString() const{
if(this->getChildren().empty()) return "<svg/>";
std::string temp="";
temp+="<svg>\n";
for(std::list<Tag*>::const_iterator it=this->getChildren().begin(); it != this->getChildren().end(); ++it){
temp+=(*it)->toString();
}
temp+="</svg>\n";
return temp;
}
std::list<Tag*> Tag::getChildren() const{
return children;
}
如该图所示,SVG Tag有子级,它应该toString()
在第一次迭代中调用TagG,但是它甚至没有正确设置迭代器,因为TagG有2个子级,而取消引用的迭代器有0个子级且很奇怪属性。有人可以告诉我我做错了什么吗?谢谢!
可能您的函数getChildren
按值返回,然后
std::list<Tag*>::const_iterator it=this->getChildren().begin();
和
it != this->getChildren().end();
点到开始和结束的不同的容器。从您的编辑中很明显,这就是这种情况,因此请进行以下更改:
std::list<Tag*>& Tag::getChildren() const{
return children;
}
或这样做:
std::list<Tag*> children = this->getChildren(); //now you ensure you work on
//a single and the same container
for(std::list<Tag*>::const_iterator it=children.begin();
it != this->children.end(); ++it){
temp+=(*it)->toString();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句