error with boost::shared_ptr<T> with incomplete T, VC7.1 ?

  • Thread starter Philippe Guglielmetti
  • Start date
P

Philippe Guglielmetti

I just ported old (VC6) working code to VC7.1 and have trouble with
something like:

class A; // forward
typedef boost::smart_ptr<A> Aptr;
class B{
Aptr a;
virtual ~B(); // implemented after A has been defined
};

I get an "use of undefined type 'A'' in checked_delete.hpp at
template<class T> inline void checked_delete(T * x)

{

typedef char type_must_be_complete[sizeof(T)];

delete x;

}

what's wrong ? thanks!
 
D

David B. Held

Philippe Guglielmetti said:
I just ported old (VC6) working code to VC7.1 and have
trouble with something like:
[...]

The best place to ask these questions is on the Boost user
list. Or, if you prefer a news interface, try GMANE
(news.gmane.org).

Dave
 
T

tom_usenet

I just ported old (VC6) working code to VC7.1 and have trouble with
something like:

class A; // forward
typedef boost::smart_ptr<A> Aptr;
shared_ptr?

class B{
Aptr a;
virtual ~B(); // implemented after A has been defined

The destructor doesn't matter, since shared_ptr uses a trick that only
requires the held type to be complete where you construct the pointer.
};

I get an "use of undefined type 'A'' in checked_delete.hpp at
template<class T> inline void checked_delete(T * x)

{

typedef char type_must_be_complete[sizeof(T)];

delete x;

}

what's wrong ? thanks!

A must be complete in every place to attempt to create an Aptr. If you
do that, you shouldn't be getting the error. The example you've shown
isn't enough to diagnose the problem though.

Tom
 
P

Philippe Guglielmetti

tom_usenet said:
shared_ptr?

A must be complete in every place to attempt to create an Aptr. If you
do that, you shouldn't be getting the error. The example you've shown
isn't enough to diagnose the problem though.

What do you mean by "create" ? The error happens when compiling the line
Aptr a, which is when I *declare* the variable a.
I know the A type should be defined when the variable is *constructed*, in
B's constructors.
This was the case with VC6, where shared_ptr worked just like "regular
"pointers, where I can write:
class C {
A* a;
};
and compile this even with A not defined. What has changed in VC7.1 ?

--
Philippe Guglielmetti - www.dynabits.com
"tom_usenet" <wrote:
I just ported old (VC6) working code to VC7.1 and have trouble with
something like:

class A; // forward
typedef boost::smart_ptr<A> Aptr;
shared_ptr?

class B{
Aptr a;
virtual ~B(); // implemented after A has been defined

The destructor doesn't matter, since shared_ptr uses a trick that only
requires the held type to be complete where you construct the pointer.
};

I get an "use of undefined type 'A'' in checked_delete.hpp at
template<class T> inline void checked_delete(T * x)

{

typedef char type_must_be_complete[sizeof(T)];

delete x;

}

what's wrong ? thanks!

A must be complete in every place to attempt to create an Aptr. If you
do that, you shouldn't be getting the error. The example you've shown
isn't enough to diagnose the problem though.

Tom
 
T

tom_usenet

What do you mean by "create" ?

Construct, passing an A*.
The error happens when compiling the line
Aptr a, which is when I *declare* the variable a.
I know the A type should be defined when the variable is *constructed*, in
B's constructors.

Right, and that's the only place it need be complete for shared_ptr to
work.
This was the case with VC6, where shared_ptr worked just like "regular
"pointers, where I can write:
class C {
A* a;
};
and compile this even with A not defined. What has changed in VC7.1 ?

Lots, but this still works (the trick is a standard one - it doesn't
rely on language extensions). This compiles fine on my VC7.1 (boost
1.30.0):

#include <boost/shared_ptr.hpp>
class A;
typedef boost::shared_ptr<A> Aptr;
class B{
public:
Aptr a;
virtual ~B(); // implemented after A has been defined
};

class A
{
};

B::~B(){
}

int main()
{
B b;
b.a = Aptr(new A);
}

Are you sure you have the latest version of boost? Could you post a
complete program exhibiting the problem?

Tom
 

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

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top