Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can avoid compilation errors if you remove the method definitions from the header files and let the classes contain only the method declarations and variable declarations/definitions. The method definitions should be placed in a .cpp file (just like a best practice guideline says). </p> <p>The down side of the following solution is (assuming that you had placed the methods in the header file to inline them) that the methods are no longer inlined by the compiler and trying to use the inline keyword produces linker errors.</p> <pre><code>//A.h #ifndef A_H #define A_H class B; class A { int _val; B* _b; public: A(int val); void SetB(B *b); void Print(); }; #endif //B.h #ifndef B_H #define B_H class A; class B { double _val; A* _a; public: B(double val); void SetA(A *a); void Print(); }; #endif //A.cpp #include "A.h" #include "B.h" #include &lt;iostream&gt; using namespace std; A::A(int val) :_val(val) { } void A::SetB(B *b) { _b = b; cout&lt;&lt;"Inside SetB()"&lt;&lt;endl; _b-&gt;Print(); } void A::Print() { cout&lt;&lt;"Type:A val="&lt;&lt;_val&lt;&lt;endl; } //B.cpp #include "B.h" #include "A.h" #include &lt;iostream&gt; using namespace std; B::B(double val) :_val(val) { } void B::SetA(A *a) { _a = a; cout&lt;&lt;"Inside SetA()"&lt;&lt;endl; _a-&gt;Print(); } void B::Print() { cout&lt;&lt;"Type:B val="&lt;&lt;_val&lt;&lt;endl; } //main.cpp #include "A.h" #include "B.h" int main(int argc, char* argv[]) { A a(10); B b(3.14); a.Print(); a.SetB(&amp;b); b.Print(); b.SetA(&amp;a); return 0; } </code></pre>
    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. 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