字符串没有声明?

时间:2013-04-08 15:32:56

标签: c++

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

这段代码是我项目“functions.h”的一部分。这说:

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

我不明白为什么这么说。我尝试使用std::string和nope。谁知道会发生什么?如果您需要更多其他信息,请询问。

2 个答案:

答案 0 :(得分:5)

正确的标头是<string>。将include指令更改为:

#include <string>

C ++标准库标题.h结尾。

执行using namespace std;被认为是非常糟糕的做法,尤其是在头文件中。这会使用std命名空间中的名称污染全局命名空间,并将所述污染传播到包含它的任何文件。

答案 1 :(得分:2)

在C中,

#include <string.h>

为您提供C字符串标题(strlen()strcmp()等。)

在C ++中,

#include <string.h>
不推荐使用

,但为您提供相同的C字符串标头。我们鼓励您使用

#include <cstring>

相反,它为您提供相同的功能,但在std::命名空间(它们所属的位置)。

如果你想要std::string,面向对象的自动分配自动扩展C ++的优点,你必须:

#include <string>

请不要将using namespace特别是std::结合使用。我们的想法是显式关于给定标识符来自哪个命名空间。

编辑: Seconding sftrabbit,输入的速度比我快。虽然{。{1}}可能在你的.cpp文件中是可以原谅的,但在标题中它是一个大写的攻击,因为包含你的标题可能会使完全有效的C ++代码突然失效,因为你改变了命名空间。

相关问题