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

Discussion in 'C++' started by Philippe Guglielmetti, Oct 9, 2003.

  1. 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!
    --
    Philippe Guglielmetti - www.dynabits.com
    Philippe Guglielmetti, Oct 9, 2003
    #1
    1. Advertising

  2. "Philippe Guglielmetti" <> wrote in message
    news:3f8513f0$0$3662$...
    > 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



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
    David B. Held, Oct 9, 2003
    #2
    1. Advertising

  3. Philippe Guglielmetti

    tom_usenet Guest

    On Thu, 9 Oct 2003 09:53:43 +0200, "Philippe Guglielmetti"
    <> 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
    tom_usenet, Oct 9, 2003
    #3
  4. "tom_usenet" wrote:

    > shared_ptr?


    yes, sure, sorry : typedef boost::shared_ptr<A> Aptr;

    > >class B{
    > >Aptr a;
    > >};
    > >
    > >I get an "use of undefined type 'A'' in checked_delete.hpp


    > 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
    Philippe Guglielmetti, Oct 9, 2003
    #4
  5. Philippe Guglielmetti

    tom_usenet Guest

    On Thu, 9 Oct 2003 17:58:56 +0200, "Philippe Guglielmetti"
    <> wrote:

    >"tom_usenet" wrote:
    >
    >> shared_ptr?

    >
    >yes, sure, sorry : typedef boost::shared_ptr<A> Aptr;
    >
    >> >class B{
    >> >Aptr a;
    >> >};
    >> >
    >> >I get an "use of undefined type 'A'' in checked_delete.hpp

    >
    >> 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" ?


    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
    tom_usenet, Oct 9, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moonlit
    Replies:
    12
    Views:
    679
    Jonathan Mcdougall
    Oct 8, 2003
  2. Alan Johnson
    Replies:
    1
    Views:
    447
    Alf P. Steinbach
    May 13, 2006
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,725
    Kai-Uwe Bux
    Jun 2, 2006
  4. Colin Caughie
    Replies:
    1
    Views:
    695
    Shooting
    Aug 29, 2006
  5. Replies:
    7
    Views:
    1,095
    Juha Nieminen
    Nov 29, 2007
Loading...

Share This Page