我收到错误:undefined reference to SE::ShockEvent<sf::Event>::ShockEvent(sf::Event&)
这样做时:
template <class Type> class ShockEvent
{
public:
//event_type will be the enumerator that tells what the event is
//so we know how to process it
explicit ShockEvent(Type& event_type);
virtual ~ShockEvent() {};
protected:
virtual std::unique_ptr<ShockEvent> createEvent(Type eventType);
private:
ShockEvent();
};
class ShockUserEvent : public ShockEvent<sf::Event>
{
public:
ShockUserEvent();
private:
sf::Event m_event;
};
ShockUserEvent::ShockUserEvent() :
ShockEvent<sf::Event>(m_event)
{
}
在 ShockUserEvent 中,我试图定义 Type,但它一直吐出错误。感谢您提供的所有帮助。
最佳答案
你需要改变这个:
explicit ShockEvent(Type& event_type);
对此:
explicit ShockEvent(Type& event_type) {}
否则它只是一个没有实现的声明。
更深入一点,如果您将 event_type
传递给此构造函数,可能需要它来做某事,因此您应该在 {
和 之间使用它>}
或丢弃该参数。
关于c++ - 定义模板构造器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860280/