C++ 为什么我能';你不用绳子吗?
我有下面的代码C++ 为什么我能';你不用绳子吗?,c++,string,cin,C++,String,Cin,我有下面的代码 #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 << "!" <<
#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;
}
#包括
#包括
main(){
std::cout>x;
std::cout必须指定名称空间:将string
更改为std::stringstd::string
非string
侧注:它是int main()
,基本main()
适用于这样的小型项目,您可以在包含后立即使用命名空间std;
执行操作。看起来您遵循的是一个古老的教程,需要从本世纪升级到其他版本。答案在错误消息中:“注:建议的备选方案:…'std::string'”另外,这是因为 String 在本机C++中不存在,只能在<代码> <代码>中定义,因为它位于 STD< /Cord>命名空间中,所以必须编写STD::String而不是String。或者使用一个定义:<代码>定义字符串STD::String …或者你可以使用“代码>使用STD::String ”我从未见过C++中的但是你知道的更多……THX不要忘记@ BJSistad,你也应该写“代码> int”()/Cord>不仅仅是Maple,它被认为是坏代码。
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;
^