"using namespace std;" 不工作

ama*_*n94 -1 c++

所以我昨晚开始学习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]
Run Code Online (Sandbox Code Playgroud)

无论如何这里是代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

哦,PS.如果它有帮助,我使用Sublime Text 2和GNU C++编译器

0x4*_*2D2 8

您需要包含字符串标头:

#include <string>
Run Code Online (Sandbox Code Playgroud)

但为什么要using namespace std声明呢?只需单独使用对象:

using std::cout;
using std::string;
Run Code Online (Sandbox Code Playgroud)


tao*_*ocp 6

#include <string>
Run Code Online (Sandbox Code Playgroud)

string来自<string>标题的你的包含.

同时,使用它是不好的做法using namespace std(参见为什么使用命名空间std被认为是一种不好的做法),你最好使用:

std::string input
std::cout, std::endl
Run Code Online (Sandbox Code Playgroud)

代替.