這是我的代碼,用於比較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標籤,無需每次重新編譯。
請查看它顯示錯誤的位置....以及您實際更改代碼的位置。 – Nawaz
只是一個猜測:'const的自動&' –
@DieterLücking'''const的自動ID = node.child_value(tagMap [3] .c_str());'''喜歡這個?我得到相同的錯誤 –