RapidXML打印头有未定义的方法

RapidXML print header has undefined methods

本文关键字:方法 未定义 打印头 RapidXML      更新时间:2023-10-16

我一直在我的一个项目中使用RapidXML。一切都进行得很顺利,直到我决定用它来编写xml。我的代码大致如下:

//attempt to open the file for writing
std::ofstream file(fileName.c_str());
if (!file.is_open())
    return false; //the file didn't open
xml_document<> doc;
//creates the contents of the document...
//...
//...
//write the document out to the file
file << doc; //if I remove this line it compiles...but I kinda need this line to output to the file
file.close();

在编译时,我得到以下错误:

In file included from ../Engine/xmlfileloader.cpp:12:0:
../Engine/include/rapidxml_print.hpp: In instantiation of 'OutIt rapidxml::internal::print_node(OutIt, const rapidxml::xml_node<Ch>*, int, int) [with OutIt = std::ostream_iterator<char, char, std::char_traits<char> >; Ch = char]':
../Engine/include/rapidxml_print.hpp:390:57:   required from 'OutIt rapidxml::print(OutIt, const rapidxml::xml_node<Ch>&, int) [with OutIt = std::ostream_iterator<char, char, std::char_traits<char> >; Ch = char]'
../Engine/include/rapidxml_print.hpp:403:9:   required from 'std::basic_ostream<Ch>& rapidxml::print(std::basic_ostream<Ch>&, const rapidxml::xml_node<Ch>&, int) [with Ch = char]'
../Engine/include/rapidxml_print.hpp:414:31:   required from 'std::basic_ostream<Ch>& rapidxml::operator<<(std::basic_ostream<Ch>&, const rapidxml::xml_node<Ch>&) [with Ch = char]'
../Engine/xmlfileloader.cpp:400:13:   required from here
../Engine/include/rapidxml_print.hpp:115:17: error: 'print_children' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:169:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_children(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:120:17: error: 'print_element_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:242:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_element_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:125:17: error: 'print_data_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:208:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_data_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:130:17: error: 'print_cdata_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:219:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_cdata_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:135:17: error: 'print_declaration_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:298:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_declaration_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:140:17: error: 'print_comment_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:321:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_comment_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:145:17: error: 'print_doctype_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:339:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_doctype_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
../Engine/include/rapidxml_print.hpp:150:17: error: 'print_pi_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
../Engine/include/rapidxml_print.hpp:361:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_pi_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit

我不明白的是为什么它会以这种方式破裂。通过rapidxml_print.hpp头文件,我看到了在print_node函数中引用的所有上述提到的函数。稍后定义print_children、print_element_node等函数,它看起来很好。我哪里做错了?

对于那些来这里寻找解决同样问题的方法的人,我有一个解决方案。查看http://gcc.gnu.org/gcc-4.7/porting_to.html下的"名称查找更改"(每n.m.)。我更改了rapidxml_print.hpp头,使其在print_node函数声明之前具有以下内容(在我的文件中,我将其插入到第104行之后):

template<class OutIt, class Ch>
inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags);
template<class OutIt, class Ch>
inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);

现在使用GCC编译得很好,只有一个警告,因为显然print_attributes函数中没有使用int flags

如果您需要使用大于或等于4.7的gcc版本,并且您正在构建期间克隆rapidxml存储库。

替换:

#include "rapidxml.hpp"
#include "rapidxml_print.hpp"

With this:

#include "rapidxml_ext.h"

rapidxml_ext.h:

#ifndef RAPIDXML_EXT_H_
#define RAPIDXML_EXT_H_
#include "rapidxml.hpp"
/* Adding declarations to make it compatible with gcc 4.7 and greater */
namespace rapidxml {
namespace internal {
    template <class OutIt, class Ch>
    inline OutIt print_children(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_attributes(OutIt out, const xml_node<Ch>* node, int flags);
template <class OutIt, class Ch>
inline OutIt print_data_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_element_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_comment_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template <class OutIt, class Ch>
inline OutIt print_pi_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
}
}
#include "rapidxml_print.hpp"
#endif /* RAPIDXML_EXT_H_ */

要消除print_attributes函数中未使用的标志,请删除该函数原型中的flags字

template<class OutIt, class Ch>
inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int);

和它的声明

template<class OutIt, class Ch>
inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int)
{