Do I have to use #include <string> beside <iostream>?

做~自己de王妃 提交于 2019-11-27 01:44:19

Yes, you have to include what you use. It's not mandated that standard headers include one another (with a few exceptions IIRC). It might work now, but might fail on a different compiler.

In your case, apparently <iostream> includes <string>, directly or indirectly, but don't rely on it.

Andy Prowl

Do I have to include the <string> header when I want to use the string type if I included the <iostream> header?

Yes, you have to. You cannot rely on relevant headers (e.g. <string>) being #included indirectly through other headers (e.g. <iostream>), although this might be the case on some implementations.

And even when this may seem to work, it could lead to troubles if not all of the relevant overloads of some operators are imported, or if a class is forward-declared in a header you #include, but information on that class being derived from some other class is only contained in a header that does not get #included.

See, for instance, this Q&A on StackOverflow for an example of such situations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!