c++ - How do I declare a class template that is placed after the main? -


i trying implement template class stack. want declare before main. can that? know compile if put main after template possible put main first template?

#include <iostream>   //start declaration of template template <class t> class stack<t>; //end declaration of template int main() {      stack<char> s(5);     s.push('a');     s.push('b');     cout <<s.pop()<<endl;     stack<double> s1(10);     s1.push(3.2);     s1.push(0.5);     cout << s1.pop()<<endl;      return 0; }   template <class t> class stack {     t *s;     int size; //how many elements can stole.     int top; //where index available.  public:       stack(int sz) {         size = sz;         s = new t[sz];         top=-1;     }      void push(t e);     t pop() {          return s[top--];     } };    template <class t> void stack<t>::push(t e) {          s[++top] = e;     } 

error given:

1>.\classtemplpers.cpp(6) : error c2143: syntax error : missing ';' before '<' 1>.\classtemplpers.cpp(6) : error c2059: syntax error : '<' 

for record, doing this:

template <class t> class stack; 

doesn't work either. see errors this:

1>.\classtemplpers.cpp(10) : error c2079: 's' uses undefined class 'stack<t>' 

the correct syntax declaring template is, quentin pointed out:

template <class t> class stack; 

but won't because:

is possible put main first template?

you if don't use template in main.

but use template, no, can't that. template must defined before can instantiated. however, may leave definitions of member functions after main if wish.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -