Thread: class template Prototype?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Question class template Prototype?

    Hi,

    I am having a problem declaring a class template prototype, i.e:

    template <class type>
    class foobar<type>; // the prototype

    // some code...

    template <class type>
    class foobar { // the definition
    // some other code...
    };

    My VC++ compiler returns (at the “class foobar<type>;” line):
    error C2143: syntax error : missing ';' before '<'

    And when I just set the prototype to “class foobar;” it returns:
    error C2989: foobar: template class has already been defined as a non-template class

    For clarity I should point out that the class prototype, definition and functions are all in
    the same file and compile fine if I don’t add the prototype and the code that requires it.
    So the question is how does one declare a class template prototype?

    Any help is greatly appreciated.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You were close...
    Code:
    template<class type> class foobar;
    template<class type> class foobar { };
    
    int main()
    {
        foobar<int> fb;
    
        return 0;
    }//main
    gg

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Smile

    thanks alot mate!!
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    here's a templated class implementing a crude templated array. The class needs to be flushed out to make it useful for anything other than demonstrating the syntax of templated class method definitions, so use it as you may:
    Code:
    template<class T> 
    class Array
    {
      public:
    	T * pT;
    	 int size;
    	 Array();
    	 Array(const Array & rhs);
    	 ~Arrray () {delete [] pT;}
    	 //etc.
    };
     
    template<class T> Array<T>::Array : size(10)
    {
      pT = new T[size];
    }
     
    template<class T> Array<T>::Array(const Array & rhs)
    {
      for(int i = 0; i < size; ++i)
    	 pT[i] = rhs.pT[i];
    }
     
    int main()
    {
       Array<int> fb;
    }
    Last edited by elad; 12-03-2004 at 03:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  3. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM