2014-01-23 126 views
0

我有以下代碼:奇怪的編譯錯誤,當

第一行是行號182

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

對於我得到以下錯誤:

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>’ 

什麼我在這裏做錯了嗎?

回答

2

make_shared是屬於命名空間std的函數,而不是std::shared_ptr<T>的成員。第二部分的錯誤信息已經非常清晰。

應該std::make_shared<FanBookPost>(fan)


和您的代碼不會看起來正確。爲什麼你需要使用shared_ptr?你應該能夠做到

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

,你應該通過const shared_ptr<Fan> &fan不僅僅是const shared_ptr<Fan> fan避免複製shared_ptr

+0

感謝,這種情況太多時間編碼的後... –