今天,偶尔写了一个小小的程序,关于字符串问题程序。
比如,我想连续打印用户输入的字符串。
#include<string> using std::cin; using std::cout; using std::endl; int main(void) { string text; while(cin >> text) cout<<text<<endl; system("pause"); return 0; }
程序编译不过,原因是:'string' : undeclared identifier,但是我已经#include<string>,预处理过string头文件,为什么不能使用string?C++中引入了名称空间,使用类时,必须指定是哪个空间的类,在这个问题上,string类属于标准库空间std,所以必须声明使用哪个空间的string类,加上一句 using std::string。这个程序就可以运行了。
热门内容