CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Foo having a boost::weak_ptr<Foo> member

    As described in the code below: it doesn't seem possible, but I could really use it... Does anyone have any ideas of how to get around this?

    Code:
    #include <iostream>
    
    #include "boost/shared_ptr.hpp"
    
    class GUI_Object;
    
    typedef boost::shared_ptr<GUI_Object> gui_object_ptr;
    typedef boost::weak_ptr<GUI_Object> weak_gui_object_ptr;
    
    class GUI_Object
    {
    public:
        GUI_Object() {}
    
        void Set_Parent(weak_gui_object_ptr p)
        {
            parent = p;
        }
    protected:
        weak_gui_object_ptr parent;
    };
    
    int main()
    {
        GUI_Object g;
        return 0;
    }
    Code:
    Error	1	error C2079: 'GUI_Object::parent' uses undefined class 'boost::weak_ptr<T>'	e:\dev\projects\test\test\main.cpp	20
    Error	2	error C2440: '=' : cannot convert from 'weak_gui_object_ptr' to 'int'	e:\dev\projects\test\test\main.cpp	17
    The reason that I am trying to do this is because I only recently added the "parent" member to my GUI_Object class. I was creating GUI_Object-derived objects on the stack, and so the pointers that other objects were holding to these objects (as their "parent") were being invalidated eventually. I thought that this could solve my issue (just means that I have to create a shared_ptr wrapper of each GUI_Object-derived object that will be a parent).

    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Foo having a boost::weak_ptr<Foo> member

    I'd say you have to include <boost/weak_ptr.hpp> as well. It seems as if your compiler does not recognize that class due to the missing include.

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Foo having a boost::weak_ptr<Foo> member

    Quote Originally Posted by Richard.J View Post
    I'd say you have to include <boost/weak_ptr.hpp> as well. It seems as if your compiler does not recognize that class due to the missing include.
    That completely slipped my mind haha. Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Foo having a boost::weak_ptr<Foo> member

    I could not believe that you miss that solution.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured