gpt4 book ai didi

c++ - 'sizeof' 对不完整类型的无效应用(创建的类)

转载 作者:搜寻专家 更新时间:2023-10-31 01:06:23 24 4
gpt4 key购买 nike

我有下一节课:

状态.hpp

    ...

class Engine;

namespace window
{

class State
{ ... } }

WConsole.hpp

    ...

class Engine;

namespace window
{

class Console: public State
{ .. } }

WMessage.hpp

    ...

class Engine;

namespace window
{

class Message: public State
{ ... } }

所有类都链接到引擎类:

引擎.hpp

    ...

namespace window
{
class State;
class Console;
class Message;
}

class Engine
{
...
std::vector< std::unique_ptr<window::State> > m_windowObjects;
std::unique_ptr<window::Console> m_consoleWindow;
std::unique_ptr<window::Message> m_messageWindow;
...
}

在 Engine.cpp 中我包含了标题:

    #include "Engine.hpp"
#include "WState.hpp"
#include "WConsole.hpp"
#include "WMessage.hpp"

如果我尝试编译我会得到这个错误:

In file included from /usr/include/c++/4.8.2/memory:81:0,
from /usr/local/include/SFGUI/Signal.hpp:6,
from /usr/local/include/SFGUI/Object.hpp:4,
from /usr/local/include/SFGUI/Widget.hpp:4,
from /usr/local/include/SFGUI/Container.hpp:4,
from /usr/local/include/SFGUI/Bin.hpp:4,
from /usr/local/include/SFGUI/SFGUI.hpp:6,
from ./include/Handler.hpp:4,
from main.cpp:1:
/usr/include/c++/4.8.2/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = window::Console]':
/usr/include/c++/4.8.2/bits/unique_ptr.h:184:16: required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = window::Console; _Dp = std::default_delete<window::Console>]'
./include/Engine.hpp:21:7: required from here
/usr/include/c++/4.8.2/bits/unique_ptr.h:65:22: error: invalid application of 'sizeof' to incomplete type 'window::Console'
static_assert(sizeof(_Tp)>0,
^
/usr/include/c++/4.8.2/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = window::Message]':
/usr/include/c++/4.8.2/bits/unique_ptr.h:184:16: required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = window::Message; _Dp = std::default_delete<window::Message>]'
./include/Engine.hpp:21:7: required from here
/usr/include/c++/4.8.2/bits/unique_ptr.h:65:22: error: invalid application of 'sizeof' to incomplete type 'window::Message'

据我所知,我没有收到与 window::State 相关的错误,这意味着我以正确的方式引入了它,但为什么我收到与 window::Console 和 window::Message 相关的错误?可能会影响类 window::Console 和 window::Message 派生自 window::State?

最佳答案

我修改为 shared_ptr,但包含 header 仍然是一个选项。

这是可能有相同错误的人的链接 here

关于c++ - 'sizeof' 对不完整类型的无效应用(创建的类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20934195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com