From: pm on
Hi,

I have a problem to compile following code

class RCBase
{
long mValue;
virtual void Destroy(void) = 0;
virtual void DeleteThis(void) = 0;
protected:
RCBase(): mValue(1){};
public:
virtual ~RCBase(void) {};
};

template<class T>
class RC_del : public RCBase
{
T* mPtr;

virtual void Destroy()
{
mPtr->~T();
delete mPtr;
}

virtual void DeleteThis()
{
free( this );
}
public:
RC_del( T* aPx ) : RCBase() , mPtr( aPx ) {}
};

template <typename T>
class CPtr
{
T* mPtr;
RCBase* mCount;
public:
CPtr(T* aPtr)
: mPtr(NULL) , mCount(NULL)
{
if ( aPtr ){
size_t rcSize = sizeof(RC_del<T>);
RC_del<T>* prc = (RC_del<T>*)malloc( rcSize );
if ( prc ){
new( prc ) RC_del<T>( aPtr );
mPtr = aPtr;
mCount = prc;
}
}
}
};

class Tt
{
int ma;
float mb;
public:
Tt() : ma(12) , mb(3.14f) {}
};

int _tmain(int argc, _TCHAR* argv[])
{
Tt* p = new Tt();
CPtr<Tt> v2(p);
return 0;
}

I get error error C2661: 'operator new' : no overloaded function takes 2
arguments for line new( prc ) RC_del<T>( aPtr );
I just want to call constructor.

Any suggestion.

PM-
From: Igor Tandetnik on
pm wrote:
> template<class T>
> class RC_del : public RCBase
> {
> T* mPtr;
>
> virtual void Destroy()
> {
> mPtr->~T();
> delete mPtr;

That would call ~T() twice - once explicitly, and again as part of executing delete statement.

> template <typename T>
> class CPtr
> {
> T* mPtr;
> RCBase* mCount;
> public:
> CPtr(T* aPtr)
> : mPtr(NULL) , mCount(NULL)
> {
> if ( aPtr ){
> size_t rcSize = sizeof(RC_del<T>);
> RC_del<T>* prc = (RC_del<T>*)malloc( rcSize );
> if ( prc ){
> new( prc ) RC_del<T>( aPtr );
> mPtr = aPtr;
> mCount = prc;

Out of curiosity - why are you doing all this? What's wrong with

mPtr = new RC_del<T>(aPtr);

Also - wouldn't CPtr need a destructor?

> I get error error C2661: 'operator new' : no overloaded function takes 2
> arguments for line new( prc ) RC_del<T>( aPtr );

#include <new>

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Victor Bazarov on
pm wrote:
> I have a problem to compile following code
>
> [..code uses placement new..]
>
> I get error error C2661: 'operator new' : no overloaded function takes 2
> arguments for line new( prc ) RC_del<T>( aPtr );
> I just want to call constructor.
>
> Any suggestion.

Try

#include <memory>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
From: pm on
Igor Tandetnik wrote:
>> virtual void Destroy()
>> {
>> mPtr->~T();
>> delete mPtr;
>
> That would call ~T() twice - once explicitly, and again as part of executing delete statement.
You are right, my mistake.

>> template <typename T>
>> class CPtr
>> {
>> T* mPtr;
>> RCBase* mCount;
>> public:
>> CPtr(T* aPtr)
>> : mPtr(NULL) , mCount(NULL)
>> {
>> if ( aPtr ){
>> size_t rcSize = sizeof(RC_del<T>);
>> RC_del<T>* prc = (RC_del<T>*)malloc( rcSize );
>> if ( prc ){
>> new( prc ) RC_del<T>( aPtr );
>> mPtr = aPtr;
>> mCount = prc;
>
> Out of curiosity - why are you doing all this? What's wrong with
>
> mPtr = new RC_del<T>(aPtr);
I've rewritten my code to minimalist and simplest way to reproduce my
compilation problem.
1. I want to use different allocator then global new.
2. I have code where thrown exception isn't allowed.

> Also - wouldn't CPtr need a destructor?
I forget to copy paste it.

>> I get error error C2661: 'operator new' : no overloaded function takes 2
>> arguments for line new( prc ) RC_del<T>( aPtr );
>
> #include <new>
>
This resolved my problems.
Thanks

PM-