VS2005 list template with own types

A

Arka

Hi,

I am trying to migrate a VS2003 VC++ Solution to VS2005.

I found a problem with the list template !

I declared my list like this:

class MyOwnTypeList : public list<MyOwnType::ptr>

where MyOwnType::ptr is declared like this:

class MyOwnType
{
public:
typedef Pointer<MyOwnType> Ptr;
....
}

The compiler makes an error on:

push_back( bst );

where bst is:

MyOwnType::ptr &bst

The error is:

error C2664: 'std::allocator<_Ty>::construct' : cannot convert
parameter 1 from 'MyOwnType *' to 'Pointer<T> *'

I must add that like another post in this group the Pointer class has
an overloaded '&' operator!

What is wrong ???

Can someone help me ???
 
T

Tom Widmer [VC++ MVP]

Arka said:
Hi,

I am trying to migrate a VS2003 VC++ Solution to VS2005.

I found a problem with the list template !

I declared my list like this:

class MyOwnTypeList : public list<MyOwnType::ptr>

where MyOwnType::ptr is declared like this:

class MyOwnType
{
public:
typedef Pointer<MyOwnType> Ptr;
...
}

The compiler makes an error on:

push_back( bst );

where bst is:

MyOwnType::ptr &bst

The error is:

error C2664: 'std::allocator<_Ty>::construct' : cannot convert
parameter 1 from 'MyOwnType *' to 'Pointer<T> *'

I must add that like another post in this group the Pointer class has
an overloaded '&' operator!

What is wrong ???

Can someone help me ???

Your Pointer<MyOwnType> doesn't meet the requirements on elements to be
stored in standard containers. In particular, it isn't
"copyconstructible". This requires (among other things) that taking the
address of an element returns the address of the element directly.

In general, avoid overloading operator&.

Tom
 
A

Arnaud Debaene

Arka said:
Hi,

I am trying to migrate a VS2003 VC++ Solution to VS2005.

I found a problem with the list template !

I declared my list like this:

class MyOwnTypeList : public list<MyOwnType::ptr>
To complete Tom's answer, it is generally a bad idea to inherit from a std
container (among other things because they do not have virtual destructors).

You'd probably better use something like :
typedef std::list<boost::shared_ptr<MyOwnType> > MyOwnTypeList;

Arnaud
 

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

Top