Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use forward class declaration in a function with template?
    primarykey
    data
    text
    <p>I have two classes. Each has a pointer pointing to the other class. So I have to use forward class declaration in one of them. This method runs well until I need to use a function with template (which I have to write both the declaration and implementation in the header file).</p> <p>More specifically, my codes are something like the following files. These files got compiled without problems on Windows by MS Visual Studio 2010. But when I compile them on Mac by XCode 4.3.2, I got an error of "Member access into incomplete type 'ClassB'" in file ClassA.h.</p> <p>Can anyone tell me how to make it compilable on Mac with XCode?</p> <p><strong>file ClassA.h</strong></p> <pre><code>#ifndef CLASS_A #define CLASS_A //#include "ClassB.h" class ClassB; class ClassA { public: ClassA(); template &lt;typename T&gt; void funcA(T *t); int iNum; ClassB *pB; }; template &lt;typename T&gt; void ClassA::funcA(T *t) { iNum = *(int*)t; iNum += pB-&gt;iNum; } #endif </code></pre> <p><strong>file ClassA.cpp</strong></p> <pre><code>#include "ClassA.h" ClassA::ClassA() { iNum = 1; } </code></pre> <p><strong>file ClassB.h</strong></p> <pre><code>#ifndef CLASS_B #define CLASS_B #include "ClassA.h" class ClassB { public: ClassB(); template &lt;typename T&gt; void funcB(T *t); int iNum; ClassA *pA; }; template &lt;typename T&gt; void ClassB::funcB(T *t) { iNum = *(int*)t; iNum -= pA-&gt;iNum; } #endif </code></pre> <p><strong>file ClassB.cpp</strong></p> <pre><code>#include "ClassB.h" ClassB::ClassB() { iNum = 2; } </code></pre> <p><strong>Main file</strong></p> <pre><code>#include &lt;stdio.h&gt; #include "ClassA.h" #include "ClassB.h" int main(void) { ClassA objA; ClassB objB; objA.pB = &amp;objB; objB.pA = &amp;objA; int a = 11; int b = 22; objA.funcA(&amp;a); objB.funcB(&amp;b); printf("Class A: %d, %d\n", objA.iNum, objA.pB-&gt;iNum); printf("Class B: %d, %d\n", objB.iNum, objB.pA-&gt;iNum); return 0; } </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