c++ - 没有匹配的成员函数来调用 child.value

标签 c++ xml parsing c++11 xml-parsing

当我尝试编译下面的代码时出现错误:

src/main.cpp:51:48: error: no matching member function for call to 'child_value'
                        std::cout << "which has value" << eb.second.child_value(kv.second);
                                                      ~~~~~~~~~~^~~~~~~~~~~

我不明白的是我能够在上面的循环中使用它。我只能假设它希望我使用 kv.second.child_value(kv.second); 代替。但是我希望它在 for (auto& eb: mapb) {.

返回的 xml 上运行这段代码。
#include "pugi/pugixml.hpp"

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

int main()
{

    const std::map<std::string, std::string> tagMap {
        {"description", "content"}, {"url", "web_address"}
    };

    pugi::xml_document doca, docb;
    std::map<std::string, pugi::xml_node> mapa, mapb;

    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("data").children("entry")) {
    const char* id = node.child_value("id");
    mapa[id] = node;
    }

    for (auto& node: docb.child("data").children("entry")) {
    const char* idcs = node.child_value("id");
        if (!mapa.erase(idcs)) {
        mapb[idcs] = node;
        }
    }

    // For removed
    for (auto& ea: mapa) {
    std::cout << "Removed:" << std::endl;
    ea.second.print(std::cout);
    }

    // For added nodes
    for (auto& eb: mapb) {
        // Loop through tag map
        for (auto& kv : tagMap) {
            // Try to find the tag name named in second map value
            // and associate it to the type of information in first map value
            std::cout << "Found" << kv.first;
            std::cout << "which has value" << eb.second.child_value(kv.second);
        }
    }

}

如果有人能解释我做错了什么,我将不胜感激。

最佳答案

根据两个重载发现here

// Get child value of current node; that is, value of the first child node of type PCDATA/CDATA
const char_t* child_value() const;

// Get child value of child with specified name. Equivalent to child(name).child_value().
const char_t* child_value(const char_t* name) const;

您需要传递一个指向字符串(或字符串文字)的指针。

std::cout << "which has value" << eb.second.child_value(kv.second.c_str());
                                                        ^^^^^^^^^^^^^^^^^

关于c++ - 没有匹配的成员函数来调用 child.value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29735239/

相关文章:

java - 向 Android 应用程序 WebView 添加后退按钮/主页功能和外部浏览器

java - 以编程方式而不是 xml 方式包含时无法获得相同的 View

java - 用java解析XML文件

xml - Golang 解析带有同名嵌套节点的 XML?

c++ - 为 C++ 编写 JSON 解析器

c++ - gcc 3.3.3 支持预编译头文件吗?

c++ - 使用 Pragma 以字节为单位适应套接字的大小

c++ - C++二维数组的访问效率

Java - 从 XML 文件中读取注释

c++ - 是否可以为 DLL/SO 构建指定特定标志/定义?