Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward declare boost smart ptr?

Tags:

c++

boost

This code fails to compile:

namespace boost
{
  template<class T> class scoped_ptr;
}

namespace FooNamespace
{

class FooClass
{
  boost::scoped_ptr<FooType> foo;
};

}

g++ says: error: field ‘foo’ has incomplete type

I thought it would be ok since I copied the scoped_ptr declaration over from the actual boost header file... What did I screw?

Note: The problem is not in FooType. I tried substituting it by int to no avail...

Thanks!

like image 404
kralyk Avatar asked Feb 08 '12 14:02

kralyk


1 Answers

Forward declarations do not work if the size of the forward-declared type must be known. Since you're embedding an instance of boost::scoped_ptr<T> in FooClass, the size of that type must be known.

You could embed a pointer instead, but that probably would defeat the purpose of scoped_ptr<T> in the first place. However, it would compile:

class FooClass
{
    boost::scoped_ptr<FooType> *foo;
};
like image 175
Frédéric Hamidi Avatar answered Oct 26 '22 16:10

Frédéric Hamidi