-
February 27th, 2010, 02:50 PM
#1
Placement new problem
We have project that requires a special memory allocator so I am trying to overload the new and placement new operators. No matter what I try, I keep getting a syntax error from VS 2005 that says:
error C2084: function 'void *operator new(size_t,void *) throw()' already has a body.
Every article I Google and every C++ book I look at makes me think what I have should at least compile. My code is:
inline void* operator new (size_t size)throw(std::bad_alloc)
{
void *p=malloc(size);
if (p==0) throw bad_alloc();
return p;
}
void* operator new (size_t size, void* p) throw()
{
if (p==0) throw std::bad_alloc();
return p;
}
It is only the new with two parameters that generates errors, which leads me to think that it is defined somewhere inside a system library. The only thing I found when googling was that if _CRTDBG_MAP_ALLOC is defined it will cause this error but I am not using that. If anyone can point out where I'm going wrong I'd appreciate it.
-
February 9th, 2011, 04:05 AM
#2
Re: Placement new problem
Same problem here.....
Please let me know if you have found the solution...
-
February 9th, 2011, 08:21 AM
#3
Re: Placement new problem
If you have that 2nd form in a header and are including the header in more than one .cpp file, then yes, you're trying to generate copies of the operator in each of those .cpp files.
The header shouldn't contain a body (body in a .cpp), or you need to declare it as inline as well like the 1st form.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|