Nit*_*mer 4 c++ templates typedef visual-c++
我正在尝试编写一个简单的模板化事件调度程序但是我遇到了我不理解的编译器错误,并且搜索它导致没有任何帮助.
我正在使用Visual Studio 2013 express.
这是我的代码:
template<typename T>
class EventDispatcher {
public:
typedef void (EventHandler)(T event);
EventDispatcher() { }
~EventDispatcher() { }
void addListener(const std::string eventName, EventHandler handler) { }
void fireEvent(T event) {}
private:
typedef std::vector<EventHandler> ListenersList;
typedef std::map<std::string, ListenersList*> ListenersMap;
ListenersMap listeners;
boost::mutex mutex;
};
Run Code Online (Sandbox Code Playgroud)
我的实际课程有点复杂,我尽可能地简化它只能让编译器抱怨所需的内容.
编译时得到的错误:
error C2535: 'void (__cdecl *std::allocator<_Ty>::address(void (__cdecl &)(T)) throw() const)(T)' : member function already defined or declared c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 548 1 TestProject
error C2535: 'void (__cdecl *std::_Wrap_alloc<std::allocator<_Ty>>::address(void (__cdecl &)(T)) const)(T)' : member function already defined or declared c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 795 1 TestProject
Run Code Online (Sandbox Code Playgroud)
知道问题是什么?
如果我注释掉这一行:ListenersMap listeners;错误消失了......
谢谢.
问题是这个:
typedef void (EventHandler)(T event);
Run Code Online (Sandbox Code Playgroud)
声明EventHandler是一个函数类型.不是指向函数的指针,实际函数.然后,您正在尝试创建一个函数向量,这当然会失败(带有适当的神秘错误消息).将typedef更改为指针:
typedef void (*EventHandler)(T event);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
196 次 |
| 最近记录: |