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

后端 未结 7 2238
清歌不尽
清歌不尽 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:54

    It is not true that the header string is included by other headers. The header string itself only has includes. No Definitions. So all necessary definitions needed for the usage of string are in headers included by the header string. These headers may already be included by other headers. Then everything works. The header ios for example includes stringbuf, which includes ...

    0 讨论(0)
  • 2020-11-22 14:57

    Even though you haven't explicitly included string, it has been included because of another standard header you included. For example vector may have included string. When you include vector, every thing from vector will get included in your file.

    I think future versions of Cpp should have a include_module or module keyword; which only include a specific module from a file. So if a file has 3 classes we only include the one we need.

    for example
    -I "../mingw/lib/include"

     module <string>
    

    Searches the directory for files that define string class. Compilation would be signicantly slower.

    0 讨论(0)
  • 2020-11-22 14:58

    If you use members that are declared inside the standard header string then yes, you have to include that header either directly or indirectly (via other headers).

    Some compilers on some platforms may on some time of the month compile even though you failed to include the header. This behaviour is unfortunate, unreliable and does not mean that you shouldn’t include the header.

    The reason is simply that you have included other standard headers which also happen to include string. But as I said, this can in general not be relied on and it may also change very suddenly (when a new version of the compiler is installed, for instance).

    Always include all necessary headers. Unfortunately, there does not appear to be a reliable online documentation on which headers need to be included. Consult a book, or the official C++ standard.

    For instance, the following code compiles with my compiler (gcc 4.6):

    #include <iostream>
    
    int main() {
        std::string str;
    }
    

    But if I remove the first line, it no longer compiles even though the iostream header should actually be unrelated.

    0 讨论(0)
  • 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 <bits/c++config.h>
    #include <ostream>
    #include <istream>
    

    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 <bits/stringfwd.h> // 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 <string>.

    0 讨论(0)
  • 2020-11-22 15:06

    If you're just using a pointer/reference to a user defined type, the type only needs to be declared:

    class my_class;
    void foo(const my_class& c);
    

    But when you're using the value, the compiler needs to know the size and with that the definition of the type.

    And keep in mind that standard headers may include other ones, which doesn't automaticly mean that all implementations do that, so you can't rely on that.

    0 讨论(0)
  • 2020-11-22 15:14

    Although, there is no direct occurence of #include <string> in a particular source file, doesn't mean it hasn't been included by another header file. Consider this:

    File: header.h

    #if !defined(__HEADER_H__)
    #define __HEADER_H__
    
    // more here
    #include <string>
    // ...and here
    
    #endif
    

    File: source1.cc

    #include <string>
    
    void foo()
    {
        // No error here.
        string s = "Foo";
    }
    

    File: source2.cc

    #include <header.h>
    
    void bar()
    {
        // Still no error, since there's a #include <string> in header.h
        string s = "Bar";
    }
    

    File: source3.cc

    void zoid()
    {
        // Here's the error; no such thing as "string", since non of the
        // previous headers had been included.
        string s = "Zoid";
    }
    
    0 讨论(0)
提交回复
热议问题