我有下面的代码为什么我不能使用字符串?
#include <iostream>
#include <string>
main(){
std::cout << "What's Your Name? ";
string x = "";
std::cin >> x;
std::cout << std::endl << "Nice to meet you " << x << "!" << std::endl;
}
和我得到这个错误
Running /home/ubuntu/workspace/client.cpp
/home/ubuntu/workspace/client.cpp: In function ‘int main()’:
/home/ubuntu/workspace/client.cpp:6:5: error: ‘string’ was not declared in this scope
string x = "";
^
/home/ubuntu/workspace/client.cpp:6:5: note: suggested alternative:
In file included from /usr/include/c++/4.8/iosfwd:39:0,
from /usr/include/c++/4.8/ios:38,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from /home/ubuntu/workspace/client.cpp:1:
/usr/include/c++/4.8/bits/stringfwd.h:62:33: note: ‘std::string’
typedef basic_string<char> string;
^
/home/ubuntu/workspace/client.cpp:6:12: error: expected ‘;’ before ‘x’
string x = "";
^
/home/ubuntu/workspace/client.cpp:7:17: error: ‘x’ was not declared in this scope
std::cin >> x;
^
为什么我不能够使用一个字符串?我正在使用Cloud9运行它,而且对于C++来说还是比较新的。如何将cin
的输入传输到字符串x
?
'的std :: string'不'string' – jaggedSpire
附注:这是'INT主要()',裸机'的main()'是[生病 - 形式](http://stackoverflow.com/questions/331148/does-c-allow-default-return-types-for-functions) – dhke
对于这样的小项目,你可以做一个'使用命名空间标准;'右包括之后。 – sp2danny