返回一個shared_ptr試圖從一個函數返回一個shared_ptr當我得到: 引用局部變量「配方」返回[-Werror =返回本地地址]C++:如何從功能
放哪兒我出錯 ?
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
是的,顯示函數聲明,請 – grisha
請發佈一個[最小的,完整的和可驗證的例子](http://stackoverflow.com/help/mcve)來重現錯誤。 – Csq