C++ 模板实例化歧义
我在玩一个FSM的游戏,遇到了一个模棱两可的问题,如下所示:C++ 模板实例化歧义,c++,C++,我在玩一个FSM的游戏,遇到了一个模棱两可的问题,如下所示: /home/permal/code/FSM/Test/../FSM/dist/include/FSM.h: In instantiation of ‘void fsm::FSM<FSMBaseState>::Event(std::unique_ptr<EventType>) [with EventType = AddEvent; FSMBaseState = EventBaseState]’: /home/pe
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h: In instantiation of ‘void fsm::FSM<FSMBaseState>::Event(std::unique_ptr<EventType>) [with EventType = AddEvent; FSMBaseState = EventBaseState]’:
/home/permal/code/FSM/Test/test.cpp:83:44: required from here
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:59:4: error: request for member ‘Event’ is ambiguous
myCurrent->Event( std::move( event ) );
^
In file included from /home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:12:0,
from /home/permal/code/FSM/Test/test.cpp:8:
/home/permal/code/FSM/Test/../FSM/dist/include/EventReceiver.h:15:15: note: candidates are: void fsm::EventReceiver<EventType>::Event(std::unique_ptr<_Tp>) [with EventType = SubtractEvent]
virtual void Event( std::unique_ptr<EventType> event ) = 0;
^
/home/permal/code/FSM/Test/../FSM/dist/include/EventReceiver.h:15:15: note: void fsm::EventReceiver<EventType>::Event(std::unique_ptr<_Tp>) [with EventType = AddEvent]
In file included from /home/permal/code/FSM/Test/test.cpp:8:0:
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h: In instantiation of ‘void fsm::FSM<FSMBaseState>::Event(std::unique_ptr<EventType>) [with EventType = SubtractEvent; FSMBaseState = EventBaseState]’:
/home/permal/code/FSM/Test/test.cpp:91:50: required from here
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:59:4: error: request for member ‘Event’ is ambiguous
myCurrent->Event( std::move( event ) );
template<typename EventType>
class EventReceiver
{
public:
virtual void Event( std::unique_ptr<EventType> event ) = 0;
};
其中,EventReceiver
定义如下:
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h: In instantiation of ‘void fsm::FSM<FSMBaseState>::Event(std::unique_ptr<EventType>) [with EventType = AddEvent; FSMBaseState = EventBaseState]’:
/home/permal/code/FSM/Test/test.cpp:83:44: required from here
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:59:4: error: request for member ‘Event’ is ambiguous
myCurrent->Event( std::move( event ) );
^
In file included from /home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:12:0,
from /home/permal/code/FSM/Test/test.cpp:8:
/home/permal/code/FSM/Test/../FSM/dist/include/EventReceiver.h:15:15: note: candidates are: void fsm::EventReceiver<EventType>::Event(std::unique_ptr<_Tp>) [with EventType = SubtractEvent]
virtual void Event( std::unique_ptr<EventType> event ) = 0;
^
/home/permal/code/FSM/Test/../FSM/dist/include/EventReceiver.h:15:15: note: void fsm::EventReceiver<EventType>::Event(std::unique_ptr<_Tp>) [with EventType = AddEvent]
In file included from /home/permal/code/FSM/Test/test.cpp:8:0:
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h: In instantiation of ‘void fsm::FSM<FSMBaseState>::Event(std::unique_ptr<EventType>) [with EventType = SubtractEvent; FSMBaseState = EventBaseState]’:
/home/permal/code/FSM/Test/test.cpp:91:50: required from here
/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:59:4: error: request for member ‘Event’ is ambiguous
myCurrent->Event( std::move( event ) );
template<typename EventType>
class EventReceiver
{
public:
virtual void Event( std::unique_ptr<EventType> event ) = 0;
};
当然,由提供的解决方案也有效。基类的方法在通过派生类实例调用时不参与“跨类”重载。 你必须把它放在一个地方
using EventReceiver<AddEvent>::Event;
using EventReceiver<SubtractEvent>::Event;
使用EventReceivermyCurrent->Event(std::move(Event))代码>这是重要的一行。myCurrent
和event
的类型是什么?在调用myCurrent->Event(std::move(Event))的函数中,这两个变量的声明准确程度如何
@AaronMcDaid这是一个问题:myCurrent
是EventBaseState
而event
是std::unique\u ptr
或std::unique\u ptr
。你可能是正确的,我会在尝试后将此标记为答案。我必须说,编译器消息真的把你引向了错误的道路——它实际上声明有两个候选对象可用,但随后忽略了它们。
using EventReceiver<AddEvent>::Event;
using EventReceiver<SubtractEvent>::Event;