有时候我们会遇到这样的情况:就是当我们要使用string类型时候,即使我们包含了#include<string>头文件,编译器仍然不识别string.造成这种情况的原因就是名空间的问题.string是定义在名空间std中的,由于我们没有自定特定的名空间,导致编译器无法确定在哪个名空间里进行搜索,所以才造成无法识别的错误.
解决方法:
1. 使用std名空间: using namespace std;
2. 在每一个需要string的前面加上域名标识:std::string //每个都要写std::,比较麻烦
3. 使用string类之前声明using std::string;