0

This is a GameBoy emulator I've been working on, and I need to include this header at this point: https://github.com/ryanterry131/JaxBoy/blob/master/src/core/memory/MemoryBus.h#L17

or else I get a rather large error that I don't understand:

In file included from src/core/GameBoy.cpp:15:
In file included from src/core/GameBoy.h:19:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2535:27: error: 
  invalid application of 'sizeof' to an incomplete type
  'Memory::MemoryController'
        static_assert(sizeof(_Tp) > 0, "default_delete can not delet...
                      ^~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2736:13: note: 
  in instantiation of member function
  'std::__1::default_delete<Memory::MemoryController>::operator()' requested
  here
        __ptr_.second()(__tmp);
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2704:46: note: 
  in instantiation of member function
  'std::__1::unique_ptr<Memory::MemoryController,
  std::__1::default_delete<Memory::MemoryController> >::reset' requested
  here
_LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
                                         ^
src/core/memory/MemoryBus.h:31:7: note: in instantiation of member function
  'std::__1::unique_ptr<Memory::MemoryController,
  std::__1::default_delete<Memory::MemoryController> >::~unique_ptr'
  requested here
class MemoryBus
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4423:26: note: 
  in instantiation of function template specialization
  'std::__1::__shared_ptr_emplace<Memory::MemoryBus,
  std::__1::allocator<Memory::MemoryBus>
  >::__shared_ptr_emplace<Core::GameBoy *>' requested here
::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4787:29: note: 
  in instantiation of function template specialization
  'std::__1::shared_ptr<Memory::MemoryBus>::make_shared<Core::GameBoy *>'
  requested here
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
                        ^
src/core/GameBoy.cpp:36:23: note: in instantiation of function template
  specialization 'std::__1::make_shared<Memory::MemoryBus, Core::GameBoy *>'
  requested here
memory_bus = std::make_shared<Memory::MemoryBus>(this);
                  ^
src/core/memory/MemoryBus.h:29:7: note: forward declaration of
  'Memory::MemoryController'
class MemoryController;

This presumably has something to do with the unique_ptr, but I don't have a damn clue what requires me to include that header in the MemoryBus header.

Thanks.

Ryan Terry
  • 181
  • 8
  • `sizeof` cant measure the size of the `MemoryController` class without the _full_ definition. forward declaration is insufficient for this. I'm not sure how the error "invalid application of 'sizeof' to an incomplete type" could be more clear about this... – Brad Allred Apr 12 '17 at 04:12
  • Is that always the case for creating a unique_ptr? – Ryan Terry Apr 12 '17 at 04:13

2 Answers2

0

On some occasions, unique_ptr needs the full definition of a class.

Take a look at Is std::unique_ptr<T> required to know the full definition of T?

Community
  • 1
  • 1
Cong Ma
  • 1,241
  • 1
  • 11
  • 20
0

The "legal" reason for the error is that unique_ptr requires a complete type.

In practice, you can circumvent the error: Declare a destructor in your class that holds the unique_ptr (Core::GameBoy?) and define the destructor in the .cpp file of the class.

GameBoy.h

class MemoryBus;

class GameBoy {
    unique_ptr<MemoryBus> foo;
public:
    ~GameBoy();
    // ...
};

GameBoy.cpp

GameBoy::~GameBoy() = default;
j6t
  • 9,150
  • 1
  • 15
  • 35