Resolved: cannot convert argument 1 from ‘std::shared_ptr‘ to ‘QtNodes::BasicGraphicsScene *’

In this post, we will see how to resolve cannot convert argument 1 from ‘std::shared_ptr<QtNodes::DataFlowGraphicsScene>’ to ‘QtNodes::BasicGraphicsScene *’


The constructor takes a QtNodes::BasicGraphicsScene* as an argument, and I have an std::shared_ptr for that object. If I try to pass the shared pointer object directly as shown above, I get the following error:
Now, If I use the .get() function, It does work, but since I am passing the raw pointer without calling the copy constructor, the reference count in my shared_ptr will not increase. Therefore, the object might get deleted while still in use.
How do I pass my object pointer while also updating the reference counter.

Best Answer:

You don’t.
You have two options to keep the object alive while the consumer is processing it:
  1. The caller takes care that the object stays around long enough.

  2. The consumer accepts a std::shared_ptr instead of a raw pointer.

In a case where the first option is not simple to achieve and there is already a std::shared_ptr, the second option is clearly preferred.

If you have better answer, please add a comment about this, thank you!