2016-08-23 7 views
-2

나는 아래의 코드문자열을 사용할 수없는 이유는 무엇입니까?

#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 문자열로 전송하려면 어떻게해야합니까?

당신은 네임 스페이스를 지정해야합니다
+8

'표준에 변화 string을 ::' – jaggedSpire

+1

사이드 메모를 string'하지 string' : 그것은'INT 주()의'베어 뼈가'주()가'병 [입니다 (http://stackoverflow.com/questions/331148/does-c-allow-default-return-types-for-functions) – dhke

+1

이런 작은 프로젝트의 경우,'namespace std;'권한을 사용할 수 있습니다. 포함 후에. – sp2danny

답변

4

: std::string

+2

추가 :'string'은 네이티브 C++에 존재하지 않으며 ''에 정의되어 있기 때문에 사용해야합니다. 거기에'std' 네임 스페이스가 있기 때문에 문자열 대신 std :: string을 써야합니다. 실제로 typedef ('typedef string std :: string')를 만들거나 정의를 사용할 수 있습니다 : #define STRING std :: string' –

+2

... 아니면'std :: string'을 사용할 수 있습니다 – jaggedSpire

+0

C++에서 보았지만 더 많이 알게되었습니다 ... thx –