Writing an allocator

P

Protoman

Why won't the compiler let me write this:

template <class T>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(pool) T; i++; return
&*j;}
private:
static T pool[1000000];
}

It keeps saying: "expected ; at end of input". Can you help me fix
this? Thanks.
 
N

Neelesh Bodas

Protoman said:
Why won't the compiler let me write this:

template <class T>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(pool) T; i++; return
&*j;}
private:
static T pool[1000000];
}

It keeps saying: "expected ; at end of input". Can you help me fix
this? Thanks.


The previous two problems. Plus the missing semicolon at the end of the
structure.
 
P

Protoman

Neelesh said:
Protoman said:
Why won't the compiler let me write this:

template <class T>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(pool) T; i++; return
&*j;}
private:
static T pool[1000000];
}


But why doesn't this work:

template <class T, long long X>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(&pool) T; i++; return
&*j;}
private:
static T pool[X];
};
template<class T,long long X> T Allocator<T>::pool[X];
int main()
{
Allocator<long long,100> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}

Any suggestions? Thanks.
 
N

Neelesh Bodas

Protoman said:
But why doesn't this work:

template <class T, long long X>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(&pool) T; i++; return
&*j;}

private:
static T pool[X];
};
template<class T,long long X> T Allocator<T>::pool[X];


should be Allocator said:
int main()
{
Allocator<long long,100> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top