Original Post
I''ve got an idea of creating a template buffer instead of making two different buffers of byte and char (ByteBuffer, CharBuffer).
Here''s the code:
'' being compiled
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(95) : error C2955: ''Buffer'' : use of class template requires template argument list
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(89) : see declaration of ''Buffer''
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(105) : error C2995: ''Buffer::Buffer'' : template function has already been defined
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(52) : see declaration of ''Buffer::Buffer''
Error executing cl.exe.
SDL_TEST.obj - 5 error(s), 0 warning(s)
Can someone clear this up, tell me how I shall write the templates.
-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor
Just dreaming wont make you (more) skilled in game programming/development.
#include <minmax.h>
template<typename T0, T1>
size_t GetSizeDiff(T0 t0, T1 t1)
{
size_t size0 = sizeof(t0);
size_t size1 = sizeof(t1);
size_t biggest = max(size0, size1);
size_t smallest = min(size0, size1);
size_t size_diff = biggest / smallest;
return size_diff;
}
template<typename Tmp>
class Buffer
{
public:
Tmp *buffer;
long length;
Tmp *ptr;
Buffer(long size)
{
buffer = (Tmp *) malloc(sizeof(Tmp) * size);
length = size;
ptr = buffer;
}
template<typename T>
Buffer(Buffer<T> *buf)
{
this->length = buf->GetSize() / GetSizeDiff(Tmp, T);
this->buffer = (Tmp *) buf->GetBuf();
this->ptr = this->buffer;
}
Tmp *GetBuf(void)
{
return buffer;
}
long *GetSize(void)
{
return &length
}
Tmp *GetPtr(size_t offset)
{
return ptr += offset;
}
void RstPtr(void)
{
ptr = buffer;
}
template<typename T>
Buffer<T> *operator=(void)
{
return new Buffer<T>(this);
}
~Buffer(void)
{
free(buffer);
}
};
template<typename T>
Buffer::Buffer(Buffer<T> *buf)
{
size_t this_size = sizeof(Tmp);
size_t that_size = sizeof(T);
size_t biggest = max(this_size, that_size);
size_t smallest = min(this_size, that_size);
size_t size_diff = biggest / smallest;
this->length = buf->GetSize() / size_diff;
this->buffer = (Tmp *) buf->GetBuf();
this->ptr = this->buffer;
}
But when I compile it I got these errors:
Compiling...
SDL_TEST.cpp
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(22) : error C2061: syntax error : identifier ''T1''
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(23) : error C2061: syntax error : identifier ''T1''
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(80) : error C2805: binary ''operator ='' has too few parameters
D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(89) : see reference to class template instantiation ''Buffer