- 论坛徽章:
- 0
|
本帖最后由 sudayly 于 2012-01-18 22:48 编辑
在写一个EventManager时,想用模板代替继承,出现了问题,求大虾看看~。看错误信息好像是,析构函数没有定义?
测试文件- #include "EventHandler.h"
- #include "LogHandler.h"
- int main(int argc, char *argv[])
- {
- EventHandler<int> *handler = new LogHandler<int>();
-
- handler->Handle(5);
-
- return 0;
- }
复制代码 EventHandler.h文件
- #ifndef EVENT_HANDLER_H
- #define EVENT_HANDLER_H
- template <typename Event>
- class EventHandler {
- public:
- virtual void Handle(Event e) = 0;
-
- virtual ~EventHandler();
- };
- #endif
复制代码 LogHandler.h文件
- #ifndef LOG_HANDER_H
- #define LOG_HANDER_H
- #include "EventHandler.h"
- #include <iostream>
- using std::cout;
- using std::ostream;
- template <typename Event>
- class LogHandler: public EventHandler<Event> {
- ostream& os_;
- public:
- LogHandler(ostream& os = cout) : os_(os) {
- }
-
- void Handle(Event e) {
- os_ << e;
- }
- ~LogHandler() {
- }
- };
- #endif
复制代码 编译出错信息为链接错误
- $ make test
- g++ test.cc -o test
- C:/DOCUME~1/lyyang/LOCALS~1/Temp/ccj2D9RZ.o:test.cc:(.text$_ZN10LogHandlerIiED0Ev[LogHandler<int>::~LogHandler()]+0x17): undefined reference to `EventHandler<int>::~EventHandler()'
- C:/DOCUME~1/lyyang/LOCALS~1/Temp/ccj2D9RZ.o:test.cc:(.text$_ZN10LogHandlerIiED1Ev[LogHandler<int>::~LogHandler()]+0x17): undefined reference to `EventHandler<int>::~EventHandler()'
- C:/DOCUME~1/lyyang/LOCALS~1/Temp/ccj2D9RZ.o:test.cc:(.rdata$_ZTV12EventHandlerIiE[vtable for EventHandler<int>]+0xc): undefined reference to `EventHandler<int>::~EventHandler()'
- C:/DOCUME~1/lyyang/LOCALS~1/Temp/ccj2D9RZ.o:test.cc:(.rdata$_ZTV12EventHandlerIiE[vtable for EventHandler<int>]+0x10): undefined reference to `EventHandler<int>::~EventHandler()'
- collect2: ld returned 1 exit status
- make: *** [test] Error 1
复制代码 |
|