在過去的一小時內一直停留在這段代碼上,仍然試圖讓我的腦袋圍繞着智能指針並實現它們,但是這個問題已經讓我很難過了。發出通過std :: unique_ptr的
void GameState::addEntity(std::unique_ptr<Entity> gameObject)
{
if(gameObject->isCollidable()){
_actors.Add(gameObject);
} else {
_props.Add(gameObject);
}
}
// This is the method the above function is trying to call.
void GameObjectManager::Add(std::unique_ptr<Entity> gameObject)
{
_gameObjects.insert(std::make_pair(ID, std::move(gameObject)));
ID++;
}
我收到的錯誤信息是;
'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
所有權轉讓應在所有點明確。 – Xeo
你是指Xeo,會不會使用參考方法進行惡作劇? – user1725794
@ user1725794:這將是微妙的。獨特指針的所有權轉讓應始終明確。這樣你最終會得到兩個共享一個唯一指針的例程,這種類型會破壞'unique_ptr'的目的(即對象應該一次擁有*一個*實體)。順便說一句,如果你想通知某個用戶,你必須在用戶名之前使用'@'字符。 –