在c ++程序上没有可行的重载'=' [英] No viable overloaded '=' on a c++ program

查看:243
本文介绍了在c ++程序上没有可行的重载'='的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some C++ code to find the differences in xml and print, using a map to rename the node tags. This is the full code:

#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 ++,我很难修复它。任何帮助或输入将被赞赏。

This is the error I get when I try and compile. I'm new to C++ and I'm having a hard time fixing it. Any help or input would be appreciated.

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


推荐答案

注意:这是一个扩展的注释,而不是直接回答您提出的问题。

Note: this is more of an extended comment than a direct answer to the question you asked.

至少如果我正确地分配了意图,这段代码:

At least if I've divined the intent correctly, this code:

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;
    }
}

...旨在产生两组对象分成 mapa mapb 中的两个组的差异。在这种情况下,我宁愿直接表达这些意图:

...is intended to produce the intersection of the two groups of objects into mapa and the difference of the two groups in mapb. That being the case, I'd rather express those intents a little more directly:

auto by_id = [](pugi::xml_node const &a, pugi::xml_node const &b) {
    return strcmp(a.get_child("id"), b.get_child("id")) == 1;
};

auto const &a = doca.child("data").children("entry");
auto const &b = docb.child("data").children("entry");

std::set_intersection(a.begin(), a.end(), 
                      a.begin(), b.end(),
                      std::inserter(mapa, mapa.end()),
                      by_id);

std::set_difference(a.begin(), a.end(),
                    b.begin(), b.end(),
                    std::inserter(mapb, mapb.end()),
                    by_id);

虽然稍微延长了一些,但我认为这表示了预期的结果足够清楚,以容易证明额外的长度。

Although marginally longer, I think this expresses the intended result enough more clearly to easily justify the extra length.

至于最初导致困难的部分:

As for the part that was originally causing you difficulty:

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 $ c> mapb ,但不要在循环中使用 eb 。同时, $ c> n.name(),但您尚未初始化 n 以实际包含任何内容。

I'll admit I'm rather confused as to what you really even intend this to accomplish. You iterate across mapb, but never use eb inside of the loop at all. At the same time, you do a search for n.name(), but you haven't initialized n to actually contain anything first.

我会猜测你真的要包括类似的东西:

I'm going to guess you really intended to include something like:

auto &n = eb.second;

...作为循环中的第一条语句不需要在循环外定义现有的 n )。

...as the first statement inside the loop (in which case you apparently don't need to define the existing n outside the loop at all).

你在 tagmaps 中的部分显然是多余的 - 具有 {id,id}

With that in place, we see that part of what you have in tagmaps is apparently redundant--having the {"id", "id"} entry doesn't change the name of those entries, so there's no real point in having it there at all.

这会让我们失望:

const std::map<std::string, std::string> tagmaps {
    {"description", "content"}
};

// ...

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

至少对我来说,看起来至少有一些机会做一些至少有用的东西,这可以更改为使用 std :: transform ,但我怀疑你获得了很多(如果有的话),但这样做。

At least to me, that looks like it at least stands some chance of doing something at least marginally useful. This could be changed to use std::transform, but I doubt you gain much (if anything) but doing so.

这篇关于在c ++程序上没有可行的重载'='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆