Jimmy
Jimmy

Reputation: 12517

Unknown type name string C++

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

#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?

Upvotes: 6

Views: 43167

Answers (3)

Masudur Rahman
Masudur Rahman

Reputation: 96

Use the following way:

std::string varName = "var value";

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

Upvotes: 1

shauryachats
shauryachats

Reputation: 10405

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

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

#include <string>
#include <cstring>

Upvotes: 12

moffeltje
moffeltje

Reputation: 4669

You have to include the string library:

#include <string>

Upvotes: 4

Related Questions