J'ai le code ci-dessousPourquoi je ne peux pas utiliser de chaîne?
#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;
}
Et je reçois cette erreur
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;
^
Pourquoi suis-je pas en mesure d'utiliser une chaîne? Je l'utilise avec Cloud9, et je suis relativement nouveau en C++. Comment puis-je transférer l'entrée de cin
à la chaîne x
?
'std :: CHAINE' pas' CHAINE' – jaggedSpire
Side note: il est 'int main()', bare-bones 'main()' est [mal -formed] (http://stackoverflow.com/questions/331148/does-c-allow-default-return-types-for-functions) – dhke
Pour les petits projets comme celui-ci, vous pouvez faire un 'using namespace std;' right après les inclus. – sp2danny