Why does omission of “#include ” only sometimes cause compilation failures?

后端 未结 7 2374
清歌不尽
清歌不尽 2020-11-22 14:23

I am a beginner with C++. When I write the code sometimes I write #include and the code works, other times I don\'t write #include

7条回答
  •  佛祖请我去吃肉
    2020-11-22 14:59

    As Branko said:

    It is possible that other headers that you do include have #include in them.

    Let's take a look at iostream includes:

    #include 
    #include 
    #include 
    

    if you check istream you can see some include so like this, we have:

    iostream => istream => ios => iosfwd

    and in iosfwd we have string library! but it's not standard, it's for forward declaration. in iosfwd we have:

    #include // For string forward declarations.

    and in stringfwd.h:

    @file bits/stringfwd.h
    This is an internal header file, included by other library headers.
    Do not attempt to use it directly. @headername{string}
    

    so, You can use string without #include .

提交回复
热议问题