成员不是结构或联盟

时间:2015-04-19 16:29:08

标签: c++ c++11

当我编译这个演示代码时,我被告知我有一个错误,我没有定义n。现在我尝试将n定义为int,但它告诉我这个错误。使用char时我也遇到了同样的问题。这是否意味着是一个字符串,如果是这样,我该如何定义它?

src/main.cpp:22:32: error: member reference base type 'char' is not a structure or union
    auto found = tagmaps.find(n.name());
                              ~^~~~~
src/main.cpp:49:24: error: member reference base type 'char' is not a structure or union
        found = tagmaps.find(n.name());
                             ~^~~~~

这是我的完整代码:

#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"}
    }
    int n;
    pugi::xml_document doca, docb;
    auto found = tagmaps.find(n.name());
    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);
    }

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

编辑:此代码用于检查是否有任何节点名称与地图中定义的名称相匹配,即它将查找名为idcontent的任何节点

1 个答案:

答案 0 :(得分:3)

您的地图需要std::string作为关键字,但您使用int,并且访问int,就好像它是某个结构/类一样。

int n;
...n.name()...

即使您将n更改为std::string,它也没有方法name()