当前位置:   article > 正文

由“error string in namespace std does not name a type”错误引发_error: ‘string’ in namespace ‘std’ does not name a

error: ‘string’ in namespace ‘std’ does not name a type

今天学习c++时遇到一个诡异的现象:当编译如下代码时编译器显示错误为:

error: 'string' in namespace 'std' does not name a type

程序如下:

  1. struct sales_data
  2. {
  3. std::string bookNo;
  4. std::string bookName;
  5. unsigned int count;
  6. double price;
  7. };
  8. int main()
  9. {
  10. return 0;
  11. }

很明显,缺少一个#include<string>

但是,如果修改代码如下所示的话,同样也能编译成功。

修改代码如下:

  1. #include<iostream>
  2. struct sales_data
  3. {
  4. std::string bookNo;
  5. std::string bookName;
  6. unsigned int count;
  7. double price;
  8. };
  9. int main()
  10. {
  11. return 0;
  12. }


这是什么原因呢?

经过google,在http://www.cplusplus.com/ 网站上查询到ios库与其他库的关系如下:

从上图可以看到iostream库里已经声明了stringstream库,包含了string的部分功能。所以出现了如此现象。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/966633
推荐阅读
相关标签
  

闽ICP备14008679号