Template in a .cpp

abelblaine111
abelblaine111 used Ask the Experts™
on
I have a functioning class using templates but it is only working when I put everything into the .h, as I have below.  I would like to split this into a .h and a .cpp file but I keep getting errors when I do.  Anyone know how to use the .cpp and .h?

 
template <class T>
class CMyTypedPtrList: public CPtrList
{
public:
      CMyTypedPtrList(){};
      virtual ~CMyTypedPtrList(){};

      BOOL CompareHead(const T searchValue) const
      {
            T pHead = (T) GetHead();
            if ( *searchValue == *pHead)
                  return TRUE;      
            return FALSE;                                                      
      };
};
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
To my knowlege its not possible. Sorry for talking in a disappointing manner. Implementation (definition) of a template class or function must be in the same file as the declaration. May be because of their macro like behaviour.
Manoj
Commented:
manoj is right - Template classes must be both declared and implemented in the .h file. This behavior is occuring probably because the compiler is trying to duplicate the implementation as well as the header code for the template you stated - When you try to use CMyTypedPtrList<CButton> for example, the compiler goes to the header file and duplicates the relevant code pieces from it. Since the compiler cant connect between the .h and the .cpp file (nothing states that a certain .cpp file is the implementation of your CMyTypedPtrList), it cannot go to any other file to duplicate code from.

However, you dont need to implement everything inline - You can define the class header, and implement it later, however still in the .h file.

In your example:


// myTypedPtrList.h
// ~~~~~~~~~~~

// declaration
template <class T>
class CMyTypedPtrList : public CPtrList
{
public:
CMyTypedPtrList() {};
vrtual ~CMyTypedPtrList(){};

BOOL CompareHead(const T searchValue) const;
};


// implementation
template <class T>
BOOL CMyTypedPtrList::CompareHead(const T searchValue) const
    {
         T pHead = (T) GetHead();
         if ( *searchValue == *pHead)
              return TRUE;    
          return FALSE;                                            
     };

Author

Commented:
Alas both your tales fill me with woe.  I'm going to keep this question open on the off chance that someone knows some workaround, otherwise I'll split the points between you with the majority going to Aviaddb for the more complete answer.  Thanks both.

Commented:

You might wanna try splitting the implementation and the declaration to .h and .cpp file, but include the .cpp file from the .h file
(from my example above), AND not adding the .cpp file to your project's compilation (if you're under VC++, that means not add it to your project). Thats to insure the compiler wont try to compile the template implementation because it has no header so its bound not to compile.

Its a messy workaround. :)

// myTypedPtrList.h

#include "myTypedPtrList.cpp"

template <class T>
class CMyTypedPtrList : public CPtrList
{
public:
CMyTypedPtrList() {};
vrtual ~CMyTypedPtrList(){};

BOOL CompareHead(const T searchValue) const;
};


// myTypedPtrList.cpp
BOOL CMyTypedPtrList::CompareHead(const T searchValue) const
   {
        T pHead = (T) GetHead();
        if ( *searchValue == *pHead)
             return TRUE;    
          return FALSE;                                            
     };

Author

Commented:
Thanks for the help both.   Thanks for the workarounds Aviadbd they helped a lot.  I was just trying to keep the code separate  to keep them easy to look at and the suggestions you gave were good.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial