triple fault
triple fault

Reputation: 14108

Strange compiler error when using shared_ptr and make_shared

I have the following code:

First line is line number 182

void FanBookServer::postRequest(const shared_ptr<Fan> fan){
    auto newPost = std::shared_ptr<FanBookPost>::make_shared(fan);
    posts.insert(newPost->getId(), *newPost);
}

For which I get the following errors:

FanBookServer.cpp: In member function ‘void mtm::FanBookServer::postRequest(std::shared_ptr<mtm::Fan>)’:
FanBookServer.cpp:183:17: error: ‘make_shared’ is not a member of ‘std::shared_ptr<mtm::FanBookPost>’
FanBookServer.cpp:183:62: error: unable to deduce ‘auto’ from ‘<expression error>’

What I'm doing wrong here?

Upvotes: 0

Views: 253

Answers (1)

Bryan Chen
Bryan Chen

Reputation: 46578

make_shared is a function belong to namespace std, not a member of std::shared_ptr<T>. the error message is already very clear about the second part.

it should be std::make_shared<FanBookPost>(fan)


and your code doesn't looks right. why you need to use shared_ptr? you should be able to do

FanBookPost newPost{fan};
posts.insert(newPost.getId(), newPost);

and you should pass const shared_ptr<Fan> &fan not just const shared_ptr<Fan> fan to avoid copy shared_ptr

Upvotes: 2

Related Questions