[英]Can't push_back an unique_ptr in a vector
我在使用此代碼時出錯:
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 <_Tp,_Dp> ::〜unique_ptr()[與_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>&)[與_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 <_Tp,_Alloc> ::〜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
您需要顯式定義類T
,而unique_ptr<T>
您聲明unique_ptr<T>
。 即包括class GameState
的標題,不要在標題game.h
轉發聲明。
這將消除error: invalid application of 'sizeof' to incomplete type 'GameState'
。
你可以在這里找到更詳細的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.