在模板状态机实现中使用不完整的类型的使用

问题描述 投票:0回答:1

这很好。但是,当我尝试在怠速内部进行以下实现进行过渡时:

class IdleState : State<StateMachine<TransitionMap, RunState, IdleState>>
{
public:
    /* Use parent constructor */
    using State<StateMachine<TransitionMap, RunState, IdleState>>::State;
    void process() override
    {
        std::cout << "IDLE" << std::endl;
        state_machine_->emitEvent<IdleState>(StartEvent{});
        /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         * This is problematic line */
    }
};

我得到汇编错误:

/usr/include/c++/9/tuple: In instantiation of ‘struct std::_Tuple_impl<0, impl::RunState, impl::IdleState>’:
/usr/include/c++/9/tuple:893:11:   required from ‘class std::tuple<impl::RunState, impl::IdleState>’
best.cpp:29:20:   required from ‘class StateMachine<impl::TransitionMap, impl::RunState, impl::IdleState>’
best.cpp:92:17:   required from here
/usr/include/c++/9/tuple:185:12: error: invalid use of incomplete type ‘class impl::RunState185 |     struct _Tuple_impl<_Idx, _Head, _Tail...>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
best.cpp:49:7: note: forward declaration of ‘class impl::RunState49 | class RunState;
      |       ^~~~~~~~
In file included from best.cpp:3:
/usr/include/c++/9/tuple:192:39: error: invalid use of incomplete type ‘class impl::RunState192 |       typedef _Head_base<_Idx, _Head> _Base;
      |                                       ^~~~~
best.cpp:49:7: note: forward declaration of ‘class impl::RunState49 | class RunState;
      |       ^~~~~~~~
In file included from best.cpp:3:
/usr/include/c++/9/tuple: In instantiation of ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = impl::RunState; _UTail = {impl::IdleState}; <template-parameter-2-3> = void; long unsigned int _Idx = 0; _Head = impl::RunState; _Tail = {impl::IdleState}]’:
/usr/include/c++/9/tuple:969:63:   required from ‘constexpr std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&) [with _U1 = impl::RunState; _U2 = impl::IdleState; typename std::enable_if<((std::_TC<true, _T1, _T2>::_MoveConstructibleTuple<_U1, _U2>() && std::_TC<true, _T1, _T2>::_ImplicitlyMoveConvertibleTuple<_U1, _U2>()) && (! std::is_same<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type, std::allocator_arg_t>::value)), bool>::type <anonymous> = true; _T1 = impl::RunState; _T2 = impl::IdleState]’
best.cpp:9:38:   required from ‘StateMachine<TM, Ss>::StateMachine() [with TM = impl::TransitionMap; Ss = {impl::RunState, impl::IdleState}]’
best.cpp:114:69:   required from here
/usr/include/c++/9/tuple:218:38: error: type ‘std::_Head_base<0, impl::RunState, false>’ is not a direct base of ‘std::_Tuple_impl<0, impl::RunState, impl::IdleState>’
  218 |    _Base(std::forward<_UHead>(__head)) { }

从我了解的内容中,有一个问题,因为尚未定义Runstate。我该如何解决? 我确实在CPPFAQ

上看到了这一点,我相信将代码分隔为
cpp
文件和

hpp

文件将起作用,但是定义实例化和包括

cpp文件对我来说是错误的。我可以其他方式解决这个问题吗?

	
当每个@Eljay建议我移动了
process()
函数的定义
在类声明之后,它效果很好:

c++ templates
1个回答
0
投票


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.