when trying to return a shared_ptr from a function I get: reference to local variable 'recipe' returned [-Werror=return-local-addr]
where did I go wrong ?
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;
}
what is the right way to return a shared_ptr ?
The function's signature isn't shown, but it sounds like it's probably returning shared_ptr<Recipe>&
. Returning a reference to a temporary is a big no-no since the referenced object will be destroyed as soon as the function exits. Just return by value instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With