即使包含字符串,也编译C ++程序的错误

时间:2015-04-17 16:07:59

标签: c++ string

我在代码上找到了一些帮助,找到了XML文件之间的差异,这是我的代码:

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

#include "pugi/pugixml.hpp"

int main() {
    pugi::xml_document doca, docb;
    std::map<string, pugi::xml_node> mapa, mapb;

    if (!doca.load_file("a.xml") || !docb.load_file("b.xml"))
        return 1;

    for (auto& node: doca.child("site_entries").children("entry")) {
        const char* id = node.child_value("id");
        mapa[new std::string(id, strlen(id))] = node;
    }

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

}

我包含了字符串,但它似乎仍然给我这个错误,我不知道为什么。谁能让我知道我做错了什么?

src/main.cpp:12:14: error: unknown type name 'string'; did you mean std::string'?
    std::map<string, pugi::xml_node> mapa, mapb;
             ^~~~~~
             std::string
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:189:65: note: 'std::string' declared here
typedef basic_string<char, char_traits<char>, allocator<char> > string;

1 个答案:

答案 0 :(得分:4)

std::map<string, pugi::xml_node> mapa, mapb;
         ^
        over here

您也需要std::string

std::map<std::string, pugi::xml_node> mapa, mapb;
            ^
         like this

您明确指出的错误消息

src/main.cpp:12:14: error: unknown type name 'string'; did you mean std::string'?
std::map<string, pugi::xml_node> mapa, mapb;
         ^~~~~~
         std::string