Common Practice for Template Function Defination - Mix with Function Declaration?
<p>Most of the time, I "avoid" to have the following style in my single header file.</p>

<pre><code>class a {
    void fun();
};

void a::fun() {
}
</code></pre>

<p>In order to avoid the following error. I try to separate class definition in cpp file and class declaration in h file. For example, the below is the wrong example :</p>

<p><hr></p>

<h2>main.cpp</h2>

<pre><code>#include "b.h"
#include "a.h"

int main()
{
    a aa;
    b bb;
}
</code></pre>

<p><hr></p>

<h2>a.h</h2>

<pre><code>#ifndef A_H
#define A_H

#include &lt;iostream&gt;

class a {
public:
    virtual int fun();
};


int a::fun()
{
    int t;
    std::cout &lt;&lt; "a" &lt;&lt; std::endl;
    return t;
}

#endif
</code></pre>

<p><hr></p>

<h2>b.h</h2>

<pre><code>#ifndef B_H
#define B_H

#include &lt;iostream&gt;
#include "a.h"

class b {
public:
    b();
};

#endif
</code></pre>

<p><hr></p>

<h2>b.cpp</h2>

<pre><code>#include "b.h"
#include "a.h"

b::b()
{
    a aa;
    aa.fun();
}
</code></pre>

<p>I will get the following error :</p>

<pre><code>1&gt;b.obj : error LNK2005: "public: virtual int __thiscall a::fun(void)" (?fun@a@@UAEHXZ) already defined in main.obj
</code></pre>

<p>However, when come to template, I will usually do it this way :</p>

<h2>a.h</h2>

<pre><code>#ifndef A_H
#define A_H

#include &lt;iostream&gt;

template &lt;typename T&gt;
class a {
public:
    virtual T fun();
};


template&lt;typename T&gt; T a&lt;T&gt;::fun()
{
    T t;
    std::cout &lt;&lt; "a" &lt;&lt; std::endl;
    return t;
}

#endif
</code></pre>

<p>May I know it this a good practice?</p>

<p>Thanks.</p>

<p>Cheok</p>
