subscript operator overloading in template class

Hello -
I'm working on a quick replacement for a STL vector (I'm not able to use STL for reasons too long to explain). My template class stores objects in a region of memory that gets reallocc'ed. He's not complete, but he does what he should right now:

#include <stdlib.h>
#include <string.h>

template<class T>
class Spartan_Vector{

public:

Spartan_Vector() : start(0), finish(0), end_of_storage(0) {}

size_t size() const { return size_t(start - finish); }
size_t capacity() const { return size_t(end_of_storage - start); }
bool empty() const { return start == finish; }


~Spartan_Vector() { free(start); }

T* start;
T* finish;
T* end_of_storage;

T& operator [](int idx) {
if (idx <= size())
return *(start + idx);
else
return NULL;
}

const T& operator [](int idx) const {
if (idx <= size())
return *(start + idx);
else
return NULL;
}

void push_back(const T& __x)
{
if (finish != end_of_storage) {
memcpy(finish, &__x, sizeof(T));
++finish;
}
else
extend(__x);
}

int extend(const T& __x)
{
T* tmp = NULL;
size_t storagelen = capacity();
size_t datalen = finish - start;
if (capacity() == 0)
{
storagelen = 1;
tmp = (T*)realloc(start, storagelen * sizeof(T));
}
else
{
storagelen = 2*capacity();
tmp = (T*)realloc(start, storagelen * sizeof(T));
}
if (NULL == tmp)
return 1;
start = tmp;
finish = start + datalen;
end_of_storage = start + storagelen;

memcpy(finish, &__x, sizeof(T));

++finish;
return 0;
}

};


Problem arises when I call the overloaded subscript. I can't do this:

struct page_info
{
int t1;
int t2;
int t3;
};

int main (int argc, char **argv)
{

Spartan_Vector<page_info> *vec = new Spartan_Vector<page_info>();
for (i = 0; i < 10; i++)
{
page_info pi = {1, 2, 3};
vec->push_back(pi);
}

page_info pi2 = {2, 2 ,2}

page_info pi = vec[1]; //THIS DOESNT COMPILE
vec[2] = pi2 //THIS DOESNT COMPILE

delete vec;
}

I can get the first line above to work if I do:

page_info *pi = (page_info *)&vec[1]

but that looks a little crazy. I'm sure I don't have my head around the overloading completely. What am I doing wrong?

Thanks
vec is a pointer, not an object.
(*vec)[index]
or
Spartan_Vector<page_info> vec;
Ahhh - right. I guess I've spent too much time in higher level languages. Many thanks.
Topic archived. No new replies allowed.