How do I declare a class template that is placed after the main?
<p>I am trying to implement a template class that is stack. But I want to declare it before main. Can I do that? I know it will compile if you put the main after the template but is it possible to put the main first then the template?</p>

<pre><code>#include &lt;iostream&gt;


//start declaration of template
template &lt;class T&gt;
class stack&lt;T&gt;;
//end declaration of template
int main() {

    stack&lt;char&gt; s(5);
    s.push('a');
    s.push('b');
    cout &lt;&lt;s.pop()&lt;&lt;endl;
    stack&lt;double&gt; s1(10);
    s1.push(3.2);
    s1.push(0.5);
    cout &lt;&lt; s1.pop()&lt;&lt;endl;

    return 0;
}


template &lt;class T&gt;
class stack {
    T *s;
    int size; //How many elements I can stole.
    int top; //Where the index is available.

public: 

    stack(int sz) {
        size = sz;
        s = new T[sz];
        top=-1;
    }

    void push(T e);
    T pop() {

        return s[top--];
    }
};



template &lt;class T&gt;
void stack&lt;T&gt;::push(T e) {

        s[++top] = e;
    }
</code></pre>

<p>Error given: </p>

<pre><code>1&gt;.\classTemplPers.cpp(6) : error C2143: syntax error : missing ';' before '&lt;'
1&gt;.\classTemplPers.cpp(6) : error C2059: syntax error : '&lt;'
</code></pre>

<p>For the record, doing this:</p>

<pre><code>template &lt;class T&gt;
class stack;
</code></pre>

<p>doesn't work either. I see errors like this: </p>

<pre><code>1&gt;.\classTemplPers.cpp(10) : error C2079: 's' uses undefined class 'stack&lt;T&gt;'
</code></pre>