C ++:我需要在这里包含这个标题,我不知道为什么

时间:2017-04-12 03:52:45

标签: c++

这是我一直在努力的GameBoy模拟器,此时我需要包含此标题:https://github.com/ryanterry131/JaxBoy/blob/master/src/core/memory/MemoryBus.h#L17

或者我得到一个我不理解的相当大的错误:

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;

这可能与unique_ptr有关,但我不知道是什么要求我在MemoryBus标题中包含该标题。

感谢。

2 个答案:

答案 0 :(得分:0)

在某些情况下,unique_ptr需要一个类的完整定义。

查看Is std::unique_ptr<T> required to know the full definition of T?

答案 1 :(得分:0)

错误的“合法”原因是unique_ptr需要完整的类型。

实际上,你可以规避错误:在你的类中声明一个包含unique_ptrCore::GameBoy?)的析构函数,并在类的.cpp文件中定义析构函数。

<强> GameBoy.h

class MemoryBus;

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

<强> GameBoy.cpp

GameBoy::~GameBoy() = default;