views:

130

answers:

2

I've got a class that has a tr1::shared_ptr as a member, like so:

class Foo
{
    std::tr1::shared_ptr<TCODBsp> bsp;

    void Bar();
}

In member function Bar, I try to assign it like this:

bsp = newTCODBsp(x,y,w,h);

g++ then gives me this error

no match for ‘operator=’ in ‘((yarl::mapGen::MapGenerator*)this)->yarl::mapGen::MapGenerator::bsp = (operator new(40u), (<statement>, ((TCODBsp*)<anonymous>)))’ /usr/include/c++/4.4/tr1/shared_ptr.h:834: note: candidates are: std::tr1::shared_ptr<TCODBsp>& std::tr1::shared_ptr<TCODBsp>::operator=(const std::tr1::shared_ptr<TCODBsp>&)

in my code, Foo is actually yarl::mapGen::MapGenerator. What am I doing wrong?

+1  A: 

You can't assign a native pointer to a shared pointer. The shared_ptr must be initialized with that value, or you can call reset() with the native pointer value.

Fred Larson
+4  A: 

call .reset(new TCODBsp) or say bsp = std::tr1::shared_ptr(new TCODBsp). Shared pointers are explicit. You can't just assign the ptr type to them.

Noah Roberts
I see. Thanks for your help.
Max