0

Can someone give me please a hint for this error:

no viable conversion from 'std::shared_ptr<Foo>' to 'std::__1::shared_ptr<Foo> *'

The QCache looks like this:

QCache<int, std::shared_ptr<Foo>> cache;

And I try to insert the element like this:

std::shared_ptr<Foo> foo;
cache.insert(23, foo);

Thanks for your help.

3
  • From the error it seems like QCache wants a pointer to it's stored type, not the type itself. Commented Oct 15, 2015 at 20:30
  • Thanks. It compiles now as a reference with: cache.insert(23, &foo); ... but I'm not sure if the smart pointer counts this reference. Commented Oct 15, 2015 at 20:34
  • Bad idea. Will lead to crashes. Check my update. Commented Oct 15, 2015 at 20:35

1 Answer 1

3

Just looked into QCache API, and since my guess is correct, I will post it as an answer (with hopes for upvotes!).

Signature for insert() is bool QCache::insert(const Key & key, T * object, int cost = 1). Moreover, API mentions the fact that QCache owns the pointer from that moment on, so you do not need shared_ptr at all. Instead, you should insert raw pointer which will be managed by QCache.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer and the help.
The problem is, that I have a vector of shared_ptr and I want to put part of them to my QCache. What happens if I set the pointer to my cache but destroy the vector and the shared pointer in it? WIll the memory be released and my cache points to nowhere?
@adapto, you will have a corrupted program if you try to do this. You can't have the same pointer owned by two managers - QCache and shared_ptr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.