我在使用此代码时遇到错误:
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&lt; _Tp&gt; :: 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&lt ; _Tp,_Dp&gt; :: reset(std :: unique_ptr&lt; _Tp,_Dp&gt; :: pointer)[with _Tp = GameState; _Dp = std :: default_delete; std :: unique_ptr&lt; _Tp,_Dp&gt; :: 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&lt; _Tp,_Dp&gt; ::〜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&LT; &gt; :: __ 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&lt; _T2&gt;&amp;)[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&lt; _Tp,_Alloc&gt; ::〜vector()[with _Tp = std :: unique_ptr; _Alloc = std :: allocator&gt;]'| ... \ 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
答案 0 :(得分:2)
使用unique_ptr
时,您需要明确定义类T
,并声明unique_ptr<T>
。即包括class GameState
的标头,请勿在标头game.h
中转发声明。
这将摆脱error: invalid application of 'sizeof' to incomplete type 'GameState'
。
您可以找到更详细的答案here。