尝试从函数返回shared_ptr时,我遇到了以下问题:
返回对局部变量“recipe”的引用[-Werror=return-local-addr]
我错在哪里了?
shared_ptr<Recipe>& Group::addRecipe(const string& groupName, unsigned int autherId, const string& recipeName){
shared_ptr<Recipe> recipe(new Recipe(recipeName, autherId));
recipes.push_back(recipe);
return recipe;
}
什么是返回shared_ptr的正确方式?
shared_ptr&
而不是shared_ptr
。只需改为返回值即可。 - dlf