2013-04-08 175 views
0
#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

一般来说,我们并不真正喜欢这里的咒骂。 – 2013-04-08 15:34:56

回答

5

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

#include <string> 

C++标准库头末与.h

using namespace std;被认为是非常糟糕的做法,特别是在头文件中。这污染了来自std命名空间名称的全局名称空间,并将所述污染传播到包含它的任何文件。

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,谁输入比我快。虽然using namespace可能在您的.cpp文件中是可以原谅的,但在头文件中,它是一种非法行为,因为包含头文件可能会使突然变得无效的C++代码失效,因为您更改了名称空间。

+0

'#include namespace charcount {0} {0} ShowPerCent(); int PerCent(); int值(char letter); int analize(std :: string var); “它说的是相同的。” – puntoinfinito 2013-04-08 15:50:54

+0

@puntoinfinito:是?!?请**做**再次阅读我的答案。 – DevSolar 2013-04-09 04:06:34