Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, All!

I'm coding array template class.

Header file is
C++
template <class T, int N>
class CMyArray
{
public:
    CMyArray(int _len=0);
    ~CMyArray();
protected:
    int	    nSize;
    int	    m_nMaxSize;
    T *	    data;
}

and cpp file is
C++
#include "MyArray.h"

template <class T, int N>
CMyArray<T, N>::CMyArray(int _len/* = 0*/) 
	: nSize(0)
	, m_nMaxSize(_len)
	, data(_len > 0 ? new T[_len] : NULL) 
{
    if(m_nMaxSize > 0) 
	memset(data, 0x00, m_nMaxSize * sizeof(T));
}

and calling code is
C++
#include "MyArray.h"

typedef CMyArray<USHORT, 256>	ArrayUShort;
ArrayUShort arrIDs;
...

then while compile, link error.
but, writing at header file without cpp file's implement,
C++
template <class T, int N>
class CMyArray
{
public:
    CMyArray(int _len=0)
        : nSize(0)
        , m_nMaxSize(_len)
        , data(_len > 0 ? new T[_len] : NULL)
    {
        if(m_nMaxSize > 0)
            memset(data, 0x00, m_nMaxSize * sizeof(T));
    }
    ~CMyArray();
protected:
    int     nSize;
    int     m_nMaxSize;
    T *     data;
}

above code has no problem.
but I want to implement in cpp file.
Posted
Comments
CPallini 30-Jun-15 7:02am    
see
https://books.google.it/books?id=EotSAwuBkJoC&pg=PA61&lpg=PA61&dq=Inclusion+model+in+templates&source=bl&ots=iwO-H5_f8b&sig=_lQy9I36pfxWuU5iIw1IDCw-uK0&hl=en&ei=guCOTMbqGJHyvQPzs9DUCw&sa=X&oi=book_result&ct=result#v=onepage&q=Inclusion%20model%20in%20templates&f=false

1 solution

You cannot put a template function definition in a cpp file. Well, you can, but if you do so, only code within that cpp file will be able to use that class/function! Otherwise, you get a linker error.

The reason for the error is that the compiler will not generate code for a template function unless it is called somewhere. If it is called within a cpp file, then the compiler tries to generate the function from it's impementation. However, if the implementation is neither in the header file nor in that particular cpp file, then it can't! As a result, the function will not be generated, and the linker will complain.
 
Share this answer
 
Comments
CPallini 30-Jun-15 7:00am    
5.
forest-321 30-Jun-15 18:54pm    
Thanks.
5.
Orjan Westin 2-Jul-15 6:42am    
You can put your class template function definition in a CPP file if you:
a) declare one or more instances of the class in the same file, and
b) you will only need those specific types.

In this case, any use of those specific types, in any other source file, will be found by the linker, so you won't get any complaints. This is quite useful for traits-based design.
Stefan_Lang 2-Jul-15 7:17am    
Indeed: by forcing an instantiation of the class, the linker will be able to find the relevant code for that specific instantiation. This is fine if you know in advance what types you're going to use the template with, but in general that is not the case, and therefore is not a practical solution!

I don't agree with your statement about traits: the whole point of generic programming is to define properties for classes that you don't even (yet) know of. Or, to put it the other way round: if you know your instantiations in advance, there is rarely a reason to complicate matters with generic programming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900