By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,483 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

Writing an allocator

P: n/a
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[i]) T; i++; return
&*j;}
private:
static T pool[1000000];
}

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

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Protoman wrote:
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[i]) 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.

Nov 22 '05 #2

P: n/a

Neelesh Bodas wrote:
Protoman wrote:
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[i]) 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[i]) 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. 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.


Nov 22 '05 #3

P: n/a

Protoman wrote:

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[i]) T; i++; return
&*j;}
To use placement new, use #include <new>

private:
static T pool[X];
};
template<class T,long long X> T Allocator<T>::pool[X];
should be Allocator<T,X> and not just Allocator<T>
int main()
{
Allocator<long long,100> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}


Hope this helps.

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.