Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to what others have said here, there are even more important problems:</p> <p>1) Large translation units lead to longer compile times and larger object file sizes.</p> <p>2) Circular dependencies! And this is the big one. And it can almost always be fixed by splitting up headers and source:</p> <pre><code>// Vehicle.h class Wheel { private: Car&amp; m_parent; public: Wheel( Car&amp; p ) : m_parent( p ) { std::cout &lt;&lt; "Car has " &lt;&lt; m_parent.numWheels() &lt;&lt; " wheels." &lt;&lt; std::endl; } }; class Car { private: std::vector&lt; Wheel &gt; m_wheels; public: Car() { for( int i=0; i&lt;4; ++i ) m_wheels.push_back( Wheel( *this ) ); } int numWheels() { return m_wheels.size(); } } </code></pre> <p>No matter what order you put these in, one will always be lacking the definition of the other, even using forward declarations it won't work, since in the function bodies are using specifics about each class's symbol.</p> <p>But if you split them up into proper .h and .cpp files and use forward declarations it will satisfy the compiler:</p> <pre><code>//Wheel.h //------- class Car; class Wheel { private: Car&amp; m_parent; public: Wheel( Car&amp; p ); }; //Wheel.cpp //--------- #include "Wheel.h" #include "Car.h" Wheel::Wheel( Car&amp; p ) : m_parent( p ) { std::cout &lt;&lt; "Car has " &lt;&lt; m_parent.numWheels() &lt;&lt; " wheels." &lt;&lt; std::endl; } //Car.h //----- class Wheel; class Car { private: std::vector&lt; Wheel &gt; m_wheels; public: Car(); int numWheels(); } //Car.cpp //------- #include "Car.h" #include "Wheel.h" Car::Car() { for( int i=0; i&lt;4; ++i ) m_wheels.push_back( Wheel( *this ) ); } int Car::numWheels() { return m_wheels.size(); } </code></pre> <p>Now the code that actually has to know specifics about the second class can just include the header file which doesn't need to know specifics about the first class.</p> <p>Headers just provide the <em>declarations</em> while source files provide the <em>definitions</em>. Or another way to say it: Headers tell you what is there (<em>what symbols are valid to use</em>) and source tells the compiler what the symbols actually do. In C++ you don't need anything more than a valid symbol to begin using whatever it is.</p> <p>Trust that C++ has a reason for this idiom, because if you don't you will make a lot of headaches for yourself down the line. I know :/</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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