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

今天学习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的部分功能。所以出现了如此现象。


版权声明:本文为u013137970原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>