2015-04-22 19 views
0

这是我的代码,用于比较XML文件,使用映射将xml标记名称定义为内容。'mapped_type'没有可行的转换

#include "pugi/pugixml.hpp" 

#include <iostream> 
#include <string> 
#include <map> 

using namespace std; 

int main() 
{ 
    pugi::xml_document doca, docb; 
    std::map<std::string, pugi::xml_node> mapa, mapb; 
    std::map<int, std::string> tagMap {make_pair(1, "data"), make_pair(2, "entry"), make_pair(3, "id"), make_pair(4, "content")}; 

    if (!doca.load_file("a.xml") || !docb.load_file("b.xml")) { 
     std::cout << "Can't find input files"; 
     return 1; 
    } 

    for (auto& node: doca.child(tagMap[1]).children(tagMap[2])) { 
     auto id = node.child_value(tagMap[3]); 
     mapa[id] = node; 
    } 

    for (auto& node: docb.child(tagMap[1]).children(tagMap[2])) { 
     auto idcs = node.child_value(tagMap[3]); 
     if (!mapa.erase(idcs)) { 
      mapb[idcs] = node; 
     } 
    } 
} 

我得到的错误是这样的:

src/main.cpp:20:30: error: no viable conversion from 'mapped_type' (aka 'std::__1::basic_string<char>') to 
     'const char_t *' (aka 'const char *') 
     for (auto& node: doca.child(tagMap[1]).children(tagMap[2])) { 
            ^~~~~~~~~ 

关于建议我尝试这样的做法:

 auto id = node.child_value(tagMap[3].c_str()); 

但我仍然得到同样的错误。看起来我试图使用地图来定义标记名称已经导致了很多问题,只是硬编码它,但映射它似乎是合乎逻辑的,因为将来我会将地图移动到一个外部文件,所以我可以运行该程序的不同XML标签,无需每次重新编译。

+1

请查看它显示错误的位置....以及您实际更改代码的位置。 – Nawaz

+0

只是一个猜测:'const的自动&' –

+0

@DieterLücking'''const的自动ID = node.child_value(tagMap [3] .c_str());'''喜欢这个?我得到相同的错误 –

回答

2

如果你读了错误密切

src/main.cpp:20:30: error: no viable conversion from 'mapped_type' (aka 'std::__1::basic_string<char>') to 
    'const char_t *' (aka 'const char *') 
    for (auto& node: doca.child(tagMap[1]).children(tagMap[2])) { 
           ^~~~~~~~~ 

你会看到,你传递一个std::string的东西,需要一个const char*。具体做法是:

doca.child(tagMap[1]) 

tagMap[1]std::stringchild()期望一个const char*。所以:

for (auto& node: doca.child(tagMap[1].c_str()).children(tagMap[2].c_str())) { 
+0

非常感谢。我尝试了这个代码,但是它给了我这个错误:src/main.cpp:21:9:错误:类型'const char *'的非const lvalue引用无法绑定到 'const char_t *'(aka'const char *') auto&id = node.child_value(tagMap [3] .c_str()); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '''请问那朵意味着它应该是'' '(常量汽配实业有限公司''' –

+0

@JamesWillson,你绑定一个非const引用到一个临时的错误状态。这是什么向你建议的解决方法是? – Barry

+0

我会尝试一些东西,回到你身边 –