2013-04-08 94 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