【问题标题】:Can't push_back an unique_ptr in a vector不能 push_back 向量中的 unique_ptr
【发布时间】:2014-02-16 17:33:06
【问题描述】:

我的代码有错误:

void Game::changeState(gameState type) // gameState is an enum
{
   if (!states.empty()) // deleting the last state
   {
       states.back()->clean();
       states.pop_back();
   }

   switch(type)
   {
       case editorState:
       {
           states.push_back(std::move(std::unique_ptr<EditorState> (new EditorState)));
           states.back()->init();
           break;
       }
       case menuState:
       {
           states.push_back(std::move(std::unique_ptr<MenuState> (new MenuState)));
           states.back()->init();
           break;
       }

   }
}

向量:

std::vector<std::unique_ptr<GameState>> states;

错误信息:

c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\unique_ptr.h||在实例化'void std ::default_delete<_tp>::operator()(_Tp*) const [with _Tp = GameState]':| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\unique_ptr.h|245|需要来自 'void std::unique_ptr <_tp _dp>::reset(std::unique_ptr<_tp _dp>::pointer) [with _Tp = GameState; _Dp = std::default_delete; std::unique_ptr<_tp _dp>::pointer = GameState*]'| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\unique_ptr.h|169|需要来自 'std::unique_ptr::~unique_ptr() [with _Tp = GameState; _Dp = std::default_delete]'| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_construct.h|95|需要来自 'void std::_Destroy (_Tp*) [with _Tp = std::unique_ptr]'| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_construct.h|105|需要'static void std:: _Destroy_aux::__destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::unique_ptr*; bool = false]'| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_construct.h|128|需要来自 'void std::_Destroy (_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::unique_ptr*]'| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_construct.h|155|需要来自 'void std::_Destroy (_ForwardIterator, _ForwardIterator, std::allocator<_t2>&) [with _ForwardIterator = std::unique_ptr*; _Tp = std::unique_ptr]'| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|403|需要来自 'std::vector::~vector() [with _Tp = std::unique_ptr; _Alloc = std::allocator >]'| ...\game.h|15|从这里需要| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\unique_ptr.h|63|错误:'sizeof' 的无效应用到不完整的类型'GameState'| ||=== 构建完成:1 个错误,12 个警告(0 分钟,1 秒)===|

当我使用默认指针时,上面的代码可以工作,但是当我使用 unique_ptr 时,它会给我上面的错误...

编辑:这是game.h:http://pastebin.com/DiBbXrC6 和游戏状态:http://pastebin.com/JD3VrktJ

【问题讨论】:

  • 首先unique_ptr会被默认移动,所以不需要std::move。其次,试试states.emplace_back(new EditorState);
  • 关于它的重要部分是 "错误:'sizeof' 对不完整类型 'GameState' 的无效应用" 所以请向我们展示上下文。这个函数Game::changeState的定义好像没有看到GameState的dtor。
  • @Joachim emplace_back 也不起作用。
  • @dyp 我将在几分钟后发送代码...
  • 最有趣的部分在 game.h 内部——上面的代码看起来与错误无关。

标签: c++ vector game-engine smart-pointers unique-ptr


【解决方案1】:

当使用unique_ptr 时,您需要在声明unique_ptr&lt;T&gt; 的任何位置显式定义T 类。 IE。包含class GameState 的标头,不要在标头game.h 中转发声明。

这将摆脱error: invalid application of 'sizeof' to incomplete type 'GameState'

你可以找到更详细的答案here

【讨论】:

  • 嗯,我认为这是问题所在,谢谢,我会尝试其他方法来改变我的状态
猜你喜欢
  • 2020-01-08
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
相关资源
最近更新 更多