c++ - 在 C++ 程序上没有可行的重载 '='

标签 c++ c++11

我有一些 C++ 代码来查找 xml 和打印中的差异,使用 map 重命名节点标签。这是完整的代码:

#include "pugi/pugixml.hpp"

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

int main()
{
    // Define mappings, default left - map on the right
    const std::map<std::string, std::string> tagmaps
    {
        {"id", "id"}, {"description", "content"}
    };

    pugi::xml_document doca, docb;
    pugi::xml_node found, n;
    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 (auto& ea: mapa) {
        std::cout << "Removed:" << std::endl;
        ea.second.print(std::cout);
        // CURL to remove entries from ES
    }

    for (auto& eb: mapb) {
        // change node name if mapping found
        found = tagmaps.find(n.name());
        if((found != tagmaps.end()) {
        n.set_name(found->second.c_str());
        }
    }

}

这是我尝试编译时遇到的错误。我是 C++ 的新手,我很难修复它。任何帮助或输入将不胜感激。

src/main.cpp:49:8: error: no viable overloaded '='
        found = tagmaps.find(n.name());

最佳答案

那是因为你分配给了错误的类型:

found = tagmaps.find(n.name());

std::map<K,V>::find const 返回 std::map<K,V>::const_iterator , pugi::xml_node 没有赋值运算符在右手边需要这样的东西。

你需要制作found正确的类型:

std::map<std::string, std::string>::const_iterator found = tagmaps.find(n.name());

或者,如果是 C++11,强烈首选:

auto found = tagmaps.find(n.name());

事实上,查看 xml_node 的引用资料,我没有看到任何 operator=完全...

关于c++ - 在 C++ 程序上没有可行的重载 '=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29730796/

相关文章:

c++ - std::is_nothrow_move_constructible 是否需要 noexcept 析构函数?

c++ - Visual Studio 下的 string::swap 复杂度

c++ - 使用 std::move 进行就地排列的性能

c++ - 为什么我不能将 <experimental/filesystem> 与 g++ 4.9.2 一起使用?

c++ - 高效实现二分查找

c++ - 无法构造对象

c++ - 运算符优先级在 C++ 中不符合预期

c++ - 在调用者方法中衰减多个函数

c++ - 为什么在 C++ 中找不到 vector

c++ - 将值插入已发布的图像?