未知类型名称字符串 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29692326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:13:37  来源:igfitidea点击:

Unknown type name string C++

c++string

提问by Jimmy

I'm new to C++ and have had some help with my program to compare two XML files. This is the code I have:

我是 C++ 的新手,并且在我的程序中得到了一些帮助来比较两个 XML 文件。这是我的代码:

#include "pugixml.hpp"

#include <iostream>
#include <unordered_map>

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

}

I seem to get a lot of errors when I try and compile it.

当我尝试编译它时,我似乎遇到了很多错误。

The first one I get is this:

我得到的第一个是这样的:

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

From what I understand, I have specified it correctly. Should I change it as it requests or is something else a-miss?

据我了解,我已经正确指定了它。我应该按照它的要求更改它还是有其他问题?

回答by shauryachats

You need to include the string library in order to use std::string.

您需要包含字符串库才能使用std::string.

Since you mentioned a lot of errors, I suspect you forgot to include <cstring>in order to use strlen().

既然你提到了很多错误,我怀疑你忘记包含<cstring>为了使用strlen().

#include <string>
#include <cstring>

回答by moffeltje

You have to include the string library:

您必须包含字符串库:

#include <string>

回答by Masudur Rahman

Use the following way:

使用以下方式:

std::string varName = "var value";

std::string varName = "var value";

I'm using Clion IDE and it worked for me.

我正在使用 Clion IDE,它对我有用。