Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown type name string C++

Tags:

c++

string

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?

like image 439
Jimmy Avatar asked Apr 17 '15 06:04

Jimmy


3 Answers

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>
like image 96
shauryachats Avatar answered Nov 20 '22 15:11

shauryachats


You have to include the string library:

#include <string>
like image 4
moffeltje Avatar answered Nov 20 '22 17:11

moffeltje


Use the following way:

std::string varName = "var value";

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

like image 1
Masudur Rahman Avatar answered Nov 20 '22 15:11

Masudur Rahman