<p>I have 2 files:</p>

<pre><code>/****demo.cpp****/
#include &lt;iostream.h&gt;
#include "gc.h"

class foo{};

int main(){
    gc&lt;foo&gt; x1;
    cout&lt;&lt;x1;
}

/*****gc.h*****/
template &lt;class T&gt; class gc 
{
    T* ptr;
public:
    operator T*(){}
};
</code></pre>

<p>If I don't write <code>operator T*(){}</code> then I get a lot of compiler errors.</p>

<p>So plz tell me what is <code>operator T*(void)</code> and when it is invoked?</p>

operator
```
T*(){}
```
is a
cast operator
.  You are providing a function that can be used to convert from a
```
gc<T>
```
to a
```
T*
```
... though it should require that you actually return something, presumably
```
ptr
```
in this case.
The problem is that the compiler doesn't know how to format your gc object to the output stream.
By providing a cast from gc to foo* it is able to output the object as just a pointer value ... probably not what you want.
You likely want to define a custom overload for the << operator to dump out your class:
```
```
template <class T>
std::ostream& operator<<( std::ostream& os, const gc<T>& x)
{
    // os << .. something useful here ..
    return os;
}
```
```