Sign in to follow this  
Followers 0
Solano

Template class problems

5 posts in this topic

Hello guys. I am making a memory manager and I have stumbled onto a very weird problem. I am making a new template class like so

CWStackAllocator.h
[code]
namespace CWolf
{
template <class T>
class CWStackAllocator
{
public:
// Constants
enum
{
CW_DEFAULT_SIZE = 100,
// Error constants
CW_ERR_UNABLE_TO_ALLOCATE = 0
};
/**
The allocated memory is stored in the form of a double linked list.
This structure represents a node in that list.
*/
struct CW_ELEMENT
{
T data;
CW_ELEMENT *pPrev;
CW_ELEMENT *pNext;
};
/**
A marker showing the top of the stack. You can only roll back to a
marker.
*/
typedef uint32_t Marker;
/**
Default constructor.
@param stackSize - Optional paramater. It tells the initial stack size.
*/
explicit CWStackAllocator(uint32_t stackSize = CW_DEFAULT_SIZE);
/**
Default destructor. Frees all of the memory used by the allocator.
Called automatically.
*/
virtual ~CWStackAllocator();
/**
This function allocates a new block of the given size from stack
top.
@param size - the size of the chunk which to allocate
*/
T* MemAlloc(uint32_t size);
/**
Get a marker to the top of the stack
@return Marker - top of the stack
*/
Marker MemGetMarker();
/**
Rolls the stack back to a previous marker
@param marker - marker showing where to roll back to
*/
void MemFreeToMarker(Marker marker);
/**
Clears the entire stack (rolls the stack back to zero).
*/
void MemClear();
private:
protected:
CW_ELEMENT *m_pFirstFree;
CW_ELEMENT *m_pFirstUsed;
uint32_t m_MaxElements;
CW_ELEMENT *m_pMemory;
};
}
[/code]

[code]
#include "CWStackAllocator.h"
namespace CWolf
{
template<class T>
CWStackAllocator<T>::CWStackAllocator(uint32_t stackSize) :
m_MaxElements(stackSize), m_pFirstUsed(0)
{
// Allocate enough memory for the maximum number of elements
char *pMemory = new char[m_MaxElements * sizeof(CW_ELEMENT)];
// Handle problems
// TODO (hentailoli#) Implement CWException
if (pMemory == 0)
throw CW_ERR_UNABLE_TO_ALLOCATE;
// Cast the memory to our internal format
//m_pMemory = (CW_ELEMENT*) pMemory;
m_pMemory = dynamic_cast<CW_ELEMENT*>(pMemory);
// Set the free list first pointer
m_pFirstFree = m_pMemory;
// Clear the memory (is this really needed?)
memset(m_pMemory, 0, sizeof(CW_ELEMENT) * m_MaxElements);
// Point at first element
CW_ELEMENT *pElement = m_pFirstFree;
// We don't have a previous element since this is the first one
pElement.pPrev = 0;
pElement.pNext = pElement + 1;
// Set the double linked free list
for(uint32_t i = 1; i < m_MaxElements; i++)
{
pElement->pPrev = pElement - 1;
pElement->pnext = pElement + 1;
pElement ++;
}
// We don't have a next element
pElement.pNext = 0;
}
template <class T>
CWStackAllocator<T>::~CWStackAllocator()
{
}
}
[/code]

Now the code compiles without a problem. I wanted to test if the constructor works so I created a file, added a main function
[code]
#include <iostream>

#include "CWStackAllocator.h"

using namespace std;
using namespace CWolf;


int main()
{
CWStackAllocator<int> test;
cout << "Hello world!" << endl;
return 0;
}
[/code]

But now when I try to compile I get the following errors:
[quote]
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CWolf::CWStackAllocator<int>::~CWStackAllocator<int>(void)" (??1?$CWStackAllocator@H@CWolf@@UAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CWolf::CWStackAllocator<int>::CWStackAllocator<int>(unsigned int)" (??0?$CWStackAllocator@H@CWolf@@QAE@I@Z) referenced in function _main
1>c:\...\MemoryManager.exe : fatal error LNK1120: 2 unresolved externals
[/quote]

Any idea why this is happening?
0

Share this post


Link to post
Share on other sites
Long story short, you can't separate a template into header and implementation source file, the full definition of the template needs to be available at point of instantiation. Basically you need to move the implementation into the header.
4

Share this post


Link to post
Share on other sites
Thanks for the fast response. Isn't this breaking the good programming practice of separating the definition and implementation? I will move my code, but its definitely something I wish could be done different :(
0

Share this post


Link to post
Share on other sites
You can still separate the declaration and the definition. The different with non-template code is that the actual template code has to be available completely to each translation unit that uses the code. One way it so stick the template declarations in the header and the template definitions in the source file, but include the source file at the end of the header file and don't compile the source. Typically you would name such a source file ".inc" or ".inl"; [i]included code[/i] or [i]inline code[/i] and they should not be compiled as your regular source files are.
3

Share this post


Link to post
Share on other sites
Oh thanks that I always wondered why people use .inl and .inc files, but never actually researched it. Well you can close the topic now.
0

Share this post


Link to post
Share on other sites
You could also put the definitions in the header file AFTER the declarations instead of using a separate include file. This still gives you a separation of interface and implementation in that the implementation is not cluttering up your interface, but is still available at the end of the same file. In my experience this is a more common practice, except in huge projects where the inl files are conditionally included only where the definitions are necessary to speed up compile times.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0