Search code examples
c++boostshared-ptrintel-edison

boost shared_ptr initialize as class member


I am having a problem initializing a boost::shared_ptr when it is a member variable of a class. I saw this previous question:

How to initialize a shared_ptr that is a member of a class?

However I still have a compiler error. Quick example code:

class A
{
    public:
      A();
};

class B
{
    public:
       B();
    private:
        boost::shared_ptr<A> mA;

        foo() {

           // the line below generates a compiler error
           mA(new A());       // ERROR

           // below will work....
           boost::shared_ptr<A> tmp(new A());    //OK
           mA = tmp;

        }
 };

The compiler complains with: error: no match for call to "(boost::shared_ptr<A>) (A*)"

However creating a tmp shared_ptr and then assigning it to mA compiles fine. I am cross-compiling on an Ubuntu 14.04 machine for an Intel Edison.

What am I missing?


Solution

  • You are looking for mA.reset(new A());

    Also shared pointer is part of the standard now, so you should use std::shared_ptr