Thread: Templates

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Templates

    Hello! I have a question that has to do with templates...

    I have the following template:
    Code:
    template <class T> class list
    {
    public:
         struct Node 
         {... ... ...};
         ...
         ...
         Node* move_back(Node* x)
         {
              return x->prev;
         }
    private:
         Node* head;
    };
    I have written this template in .h file and I want to move it to .cpp source file. My problem is that I don't know how to write the function in the .cpp file (it's just a copy-paste but how should I write the prototype of the function move_back?)

    Any kind of help will be very useful for me...

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Code:
    template <class T> Node* list<T>::move_back(Node* x)
    {
        // ...
    }
    ?

    I tend to write most of the function bodies inside the class declaration if I can. Typing template <class T>... for every member function quickly gets annoying.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Nope...
    Code:
    template <class T>
    Node* list<T>::move_back(Node* x)
    {
    	return x->prev;
    }
    4 errors at line 2:
    syntax error : missing ';' before '*'
    'Node' : missing storage-class or type specifiers
    syntax error : ';'
    'T' : undeclared identifier

    I wanted to write it inline (and it's the best solution) but in this case I have to write it in the .cpp file.
    Last edited by axydias; 04-30-2006 at 07:06 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Node is a nested class inside list, so you have to qualify it as list<T>::Node.

    >> I wanted to write it inline (and it's the best solution) but in this case I have to write it in the .cpp file.
    Remember that unless you include the cpp file in your header file, or use some other solution (see http://www.parashift.com/c++-faq-lit...html#faq-35.15) you will get linker errors when you put the function definitions in the cpp file.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    >> Node is a nested class inside list, so you have to qualify it as list<T>::Node.

    Damn I didn't notice that. I should've tried to compile it myself before replying
    *learnt lesson*
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM