CWStackAllocator.h
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;
};
}
#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()
{
}
}
Now the code compiles without a problem. I wanted to test if the constructor works so I created a file, added a main function
#include <iostream>
#include "CWStackAllocator.h"
using namespace std;
using namespace CWolf;
int main()
{
CWStackAllocator<int> test;
cout << "Hello world!" << endl;
return 0;
}
But now when I try to compile I get the following errors:
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?