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

版权声明:本文为博主原创文章,转载请注明来源!获取更新内容请关注我的开源项目:https://github.com/zcc0721/MasterBlockchain https://blog.csdn.net/u013137970/article/details/50708774

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

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

程序如下:

struct sales_data
{
	std::string bookNo;
	std::string bookName;
	unsigned int count;
	double price;
};
int main()
{
	return 0;
}

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

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

修改代码如下:

#include<iostream>
struct sales_data
{
	std::string bookNo;
	std::string bookName;
	unsigned int count;
	double price;
};
int main()
{
	return 0;
}


这是什么原因呢?

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

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

猜你喜欢

转载自blog.csdn.net/u013137970/article/details/50708774