Question

I am having an error with this code:

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;
       }

   }
}

The vector :

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

The error message:

c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\unique_ptr.h||In instantiation of '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|required from '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|required from '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|required from '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|required from '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|required from '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|required from '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|required from 'std::vector<_Tp, _Alloc>::~vector() [with _Tp = std::unique_ptr; _Alloc = std::allocator >]'| ...\game.h|15|required from here| c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\unique_ptr.h|63|error: invalid application of 'sizeof' to incomplete type 'GameState'| ||=== Build finished: 1 errors, 12 warnings (0 minutes, 1 seconds) ===|

My code above works when i use default pointers, but when i am using unique_ptr it gives me the error above ...

EDIT: Here is the game.h : http://pastebin.com/DiBbXrC6 And gamestate : http://pastebin.com/JD3VrktJ

Was it helpful?

Solution

When using unique_ptr you need to explicitly define the class T where-ever you declare the unique_ptr<T>. I.e. include the header for the class GameState, do not forward declare in the header game.h.

This will get rid of error: invalid application of 'sizeof' to incomplete type 'GameState'.

You can find more detailed answer here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top