2015-04-22 16 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

我會嘗試一些東西,回到你身邊 –