Thread: Splitting template class to header .h file and implementation .cpp file

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Splitting template class to header .h file and implementation .cpp file

    Hi.
    What is the right syntax for implementing the .cpp of a template class?

    Consider this LinkedList.h file:

    Code:
    #include<iostream>
    #include"Iterator.h"
    
    template <class T>
    class LinkedList {
    
    public:
        LinkedList();
        virtual ~LinkedList();
        // more List-related methods here...
    
    private:
    
        template <class T>
        class Node {
        public:
            Node(T d, Node<T>* n=NULL, Node<T>* p=NULL):
                data(d),
                next(n),
                prev(p){}
    
            Node<T>* next;
            Node<T>* prev;
            T data;
        };
    
        template <class T>
        class ListIterator: public Iterator<T> {
        public:
            virtual T& operator*();
            virtual T* operator->();
            virtual void operator++();
            virtual void operator--();
        private:
            Node<T>* current;
        };
    
    };
    How should the implementation for the LinkedList constructor, for example, should look like in the LinkedList.cpp file?

    I tried this:

    Code:
    #include "LinkedList.h"
    
    template <class T>
    LinkedList<T>::LinkedList<T>() {
        // constructor
    }
    
    LinkedList<T>::~LinkedList<T>() {
        // destructor
    }
    But the compiler wouldn't accept it.
    Last edited by Absurd; 09-12-2014 at 08:23 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    C Programmer here; but, I thought most templates (if not all) can NOT be split between header and cpp file.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    Hey Tim. thanks for your comment. (I love your signature... )
    So everything goes in the .h file then?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Tim is (mostly) correct. You can separate the declaration from the definition, providing that each translation unit (source file) that instantiates a template can see both. One common way is to put the declarations in a .h file, and the definitions in a .tcc or some other such file, which is included at the bottom of the .h file.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The "export" keyword was in the original C++ standard, and was intended to alleviate such things. Catch is, only a couple of compilers ever properly implemented the functionality (apparently quite difficult to do so) and it has now been deprecated in the latest standard.

    There are compilers that support "explicit template instantiation" or similar concepts which - in effect - support what you describe. Unfortunately, the specific techniques (coding needed to do it) and means of enabling such capabilities (command line options, etc) are compiler specific. And there are plenty of other gotchas too.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-05-2014, 09:23 PM
  2. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  3. Implementation of a template class functions
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2008, 03:36 AM
  4. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  5. Replies: 4
    Last Post: 11-01-2006, 02:23 PM