Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Template class problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 hentailoli   Members   -  Reputation: 185

Like
0Likes
Like

Posted 19 August 2012 - 10:16 AM

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
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


Any idea why this is happening?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9629

Like
4Likes
Like

Posted 19 August 2012 - 10:24 AM

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.

#3 hentailoli   Members   -  Reputation: 185

Like
0Likes
Like

Posted 19 August 2012 - 10:35 AM

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 :(

#4 Brother Bob   Moderators   -  Reputation: 8453

Like
3Likes
Like

Posted 19 August 2012 - 10:46 AM

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"; included code or inline code and they should not be compiled as your regular source files are.

#5 hentailoli   Members   -  Reputation: 185

Like
0Likes
Like

Posted 19 August 2012 - 10:54 AM

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.

#6 krippy2k8   Members   -  Reputation: 646

Like
0Likes
Like

Posted 19 August 2012 - 03:30 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS