首页 文章

无法push_back向量中的unique_ptr

提问于
浏览
0

我在使用此代码时出错:

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()[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 <_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'无效应用于不完整类型“游戏状态” | || ===构建完成:1个错误,12个警告(0分钟,1秒)=== |

上面的代码在我使用默认指针时有效,但是当我使用unique_ptr时它会给我上面的错误...

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

1 回答

  • 2

    使用 unique_ptr 时,您需要明确定义类 T ,其中 - 您声明 unique_ptr<T> . 即包括 class GameState 的 Headers ,不要在 Headers game.h 中转发声明 .

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

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

相关问题