Note that there are some explanatory texts on larger screens.

plurals
  1. POShould '#include' and 'using' statements be repeated in both header and implementation files (C++)?
    primarykey
    data
    text
    <p>I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my header file, my implementation file can just #include the header file, and the compiler won't mind if I don't repeat the other statements.</p> <p>What about people though?</p> <p>My main concern is that if I don't repeat the '#include', 'using', and also 'typedef' (now that I think of it) statements, it takes that information away from the file in which it's used, which could lead to confusion.</p> <p>I am just working on small projects at the moment where it won't really cause any issues, but I can imagine that in larger projects with more people working on them it could become a significant issue.</p> <p>An example follows:</p> <p><strong>UPDATE:</strong> my function prototypes for 'Unit' have string, ostream and StringSet among their return types and parameters - I am not including anything in my header file that is used only in the implementation file.</p> <pre><code>//Unit.h #include &lt;string&gt; #include &lt;ostream&gt; #include "StringSet.h" using std::string; using std::ostream; class Unit { public: //public members with string, ostream and StringSet //in their return values/parameter lists private: //private members //unrelated side-question: should private members //even be included in the header file? } ; //Unit.cpp #include "Unit.h" //The following are all redundant from a compiler perspective: #include &lt;string&gt; #include &lt;ostream&gt; #include "StringSet.h" using std::string; using std::ostream; //implementation goes here </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload