I'm having trouble breaking this up into the header and the cpp file. I've never worked with templates so most of this is gibberish. The errors seem to imply that some of the mallocator functions can't be declared as they are. Anyway, here are the errors, followed by the new files. Note that I haven't incorporated mallocator into the memManage code yet.
Errors
Code:
src\memManage.cpp:79: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:79: error: ISO C++ forbids declaration of `mallocator' with no type
src\memManage.cpp: In function `int mallocator()':
src\memManage.cpp:79: error: `int mallocator()' redeclared as different kind of symbol
src\memManage.h:18: error: previous declaration of `template<class T> class mallocator'
src\memManage.h:18: error: previous non-function declaration `template<class T> class mallocator'
src\memManage.cpp:79: error: conflicts with function declaration `int mallocator()'
src\memManage.cpp: At global scope:
src\memManage.cpp:81: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:81: error: expected `,' or `...' before '&' token
src\memManage.cpp:81: error: ISO C++ forbids declaration of `mallocator' with no type
src\memManage.cpp:81: error: ISO C++ forbids declaration of `mallocator' with no type
src\memManage.cpp: In function `int mallocator(int)':
src\memManage.cpp:81: error: `int mallocator(int)' redeclared as different kind of symbol
src\memManage.h:18: error: previous declaration of `template<class T> class mallocator'
src\memManage.h:18: error: previous non-function declaration `template<class T> class mallocator'
src\memManage.cpp:81: error: conflicts with function declaration `int mallocator(int)'
src\memManage.cpp: At global scope:
src\memManage.cpp:83: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:83: error: ISO C++ forbids declaration of `mallocator' with no type
src\memManage.cpp:83: error: declaration of template `template<class U> int mallocator(const mallocator<U>&)'
src\memManage.h:18: error: conflicts with previous declaration `template<class T> class mallocator'
src\memManage.h:18: error: previous non-function declaration `template<class T> class mallocator'
src\memManage.cpp:83: error: conflicts with function declaration `template<class U> int mallocator(const mallocator<U>&)'
src\memManage.cpp:85: error: expected constructor, destructor, or type conversion before '::' token
src\memManage.cpp:87: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:87: error: expected constructor, destructor, or type conversion before "mallocator"
src\memManage.cpp:89: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:89: error: expected constructor, destructor, or type conversion before "mallocator"
src\memManage.cpp:91: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:91: error: expected constructor, destructor, or type conversion before "mallocator"
src\memManage.cpp:98: error: `template<class T> class mallocator' used without template parameters
src\memManage.cpp:98: error: `template<class T> class mallocator' used without template parameters
memManage.h
Code:
#include <exception> // for std::bad_alloc
#include <new>
#include <cstdlib> // for malloc() and free()
#include <list>
#include <iostream> //for debugging
#include <limits>
#define DEBUG
namespace MemManage
{
extern unsigned long allocated;
extern std::list<void *> addresses;
extern std::list<std::size_t> sizes;
}
template <class T> class mallocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind {
typedef mallocator<U> other;
};
mallocator() throw();
mallocator(const mallocator&) throw();
template <class U> mallocator(const mallocator<U>&) throw();
~mallocator() throw();
pointer address(reference x) const;
const_pointer address(const_reference x) const;
pointer allocate(size_type n, const void* = 0);
void deallocate(pointer p, size_type);
size_type max_size() const throw();
void construct(pointer p, const_reference v);
void destroy(pointer p);
};
void* operator new (std::size_t size)throw ( std::bad_alloc );
void operator delete (void *p) throw ();
memManage.cpp ... only the mallocator code added at the end of the file
Code:
mallocator::mallocator() throw() {}
mallocator::mallocator(const mallocator&) throw() {}
template <class U> mallocator::mallocator(const mallocator<U>&) throw() {}
mallocator::~mallocator() throw() {}
pointer mallocator::address(reference x) const { return &x; }
const_pointer mallocator::address(const_reference x) const { return &x; }
pointer mallocator::allocate(size_type n, const void* = 0)
{
pointer p = static_cast<pointer>( malloc(n) );
if (!p) throw std::bad_alloc();
return p;
}
void mallocator::deallocate(pointer p, size_type) { free(p); }
size_type mallocator::max_size() const throw() { return std::numeric_limits<std::size_t>::max(); }
void mallocator::construct(pointer p, const_reference v) { new (p) value_type(v); }
void mallocator::destroy(pointer p) { p->~value_type(); }