2013-04-30 90 views
-1

所以我昨天晚上開始學習C++,因爲它的東西我想進入一段時間,從我看過的幾個教程,它似乎可以是一個簡單的拿起考慮我很好與PHP和有經驗的是JAVA。「using namespace std;」不工作

一個問題,寫我的第一個程序,和我的「使用命名空間標準;」聲明不會工作,當我構建程序即時得到拋出這個錯誤:

C:\\Users\\p6735a\\Desktop\\Project\\game.cpp: In function `int main(int, char *)': 
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: `string' undeclared (first use this   function) 
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: (Each undeclared identifier is reported only once 
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: for each function it appears in.) 
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: parse error before `;' 
[Finished in 0.1s with exit code 1] 

反正這裏是代碼:

#include <iostream> 
using namespace std; 

int main(int argc, char *argv[]) 
{ 
string input; 
    int hp; 

cout << "You see a man. Would you like to kill him?\n1. Yes\n2. No" << endl; 
//cin >> input; 
} 

任何幫助表示讚賞!

哦,PS。如果有幫助,即時通訊使用的崇高文本2與GNU C++編譯器

+1

您仍然需要包含必需的標題,例如'#包括' – Joe 2013-04-30 01:08:37

回答

7

需要包含字符串頭:

#include <string> 

但是,爲什麼有using namespace std聲明呢?只需單獨使用對象:

using std::cout; 
using std::string; 
+0

好了,感謝的人,但我在看didnt教程有「的#include ,他這樣說,他用coderunner的MAC, 這是否有所作爲? – amartin94 2013-04-30 01:13:23

+1

@ amartin94,這是不明其他頭文件包含了什麼頭文件,所以你應該總是包含你所需要的東西。我會對一個不這樣做的教程保持警惕[Good books](http://stackoverflow.com/questions/388242/the-definitive -c-book-guide-and-list) – chris 2013-04-30 01:16:59

+0

Thanks man。幫助被讚賞 – amartin94 2013-04-30 01:21:41

5

添加

#include <string> 

您包含自string是從<string>頭。

同時,它是壞的做法是使用using namespace std(見Why using namespace std is considered a bad practice),你最好使用:

std::string input 
std::cout, std::endl 

代替。

+0

謝謝了!希望這項作品 – amartin94 2013-04-30 01:14:09

+0

@ amartin94不客氣。 – taocp 2013-04-30 01:16:32

相關問題