puntoinfinito
puntoinfinito

Reputation: 39

String was not declared?

#include <string.h>
using namespace std;
namespace charcount 
{
    int ShowPerCent();
    int PerCent();
    int Values(char letter);
    int analize(string var);
}

This code is all part of "functions.h" of my project. This says:

functions.h: 7:13: error: 'string' was not declared in this scope

And I don't understand why says that this. I try with std::string and nope. Anyone know what happens? If you need more additional information ask.

Upvotes: 0

Views: 868

Answers (2)

DevSolar
DevSolar

Reputation: 70372

In C,

#include <string.h>

gives you the C string header (strlen(), strcmp() et al.).

In C++,

#include <string.h>

is deprecated, but gives you the same C string header. You are encouraged to use

#include <cstring>

instead, which gives you the same functions but in the std:: namespace (where they belong).

If you want std::string, the object-oriented auto-allocating auto-expanding C++ niceness, you would have to:

#include <string>

And please, don't use using namespace, especially not in combination with std::. The idea is to be explicit about which namespace a given identifier comes from.

Edit: Seconding sftrabbit, who typed quicker than me. While using namespace might be pardonable in your .cpp files, in headers it's a capital offense, because including your header could make perfectly valid C++ code invalid all of a sudden, because you changed the namespace.

Upvotes: 2

Joseph Mansfield
Joseph Mansfield

Reputation: 110748

The correct header is <string>. Change the include directive to:

#include <string>

The C++ standard library headers do not end with .h.

It's considered very bad practice to do using namespace std;, especially in a header file. This pollutes the global namespace with names from the std namespace and propagates said pollution to any file that includes it.

Upvotes: 5

Related Questions