• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
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
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
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