我收到以下链接器错误:
Error 1 error LNK2001: unresolved external symbol "public: __thiscall AguiEvent<class AguiEmptyEventArgs>::AguiEvent<class AguiEmptyEventArgs>(void)" (??0?$AguiEvent@VAguiEmptyEventArgs@@@@QAE@XZ) AguiWidgetBase.obj
Error 2 error LNK2001: unresolved external symbol "public: void __thiscall AguiEvent<class AguiEmptyEventArgs>::call(class AguiEmptyEventArgs)" (?call@?$AguiEvent@VAguiEmptyEventArgs@@@@QAEXVAguiEmptyEventArgs@@@Z) AguiWidgetBase.obj
Error 3 error LNK2001: unresolved external symbol "public: __thiscall AguiEvent<class AguiControlEventArgs>::AguiEvent<class AguiControlEventArgs>(void)" (??0?$AguiEvent@VAguiControlEventArgs@@@@QAE@XZ) AguiWidgetBase.obj
Error 4 error LNK2001: unresolved external symbol "public: void __thiscall AguiEvent<class AguiControlEventArgs>::call(class AguiControlEventArgs)" (?call@?$AguiEvent@VAguiControlEventArgs@@@@QAEXVAguiControlEventArgs@@@Z) AguiWidgetBase.obj
Error 5 fatal error LNK1120: 4 unresolved externals C:\Users\Josh\Documents\Visual Studio 2008\Projects\Agui\STATIC Release\Agui.exe
我有AguiEventArgs.h,它有:
class AguiWidgetBase;
class AguiEmptyEventArgs {
public:
AguiEmptyEventArgs();
};
class AguiMouseEventArgs {
AguiPoint position;
int mouseWheelChange;
AguiMouseButtonEnum button;
public:
AguiPoint getPosition() const;
int getMouseWheelChange() const;
AguiMouseButtonEnum getButton() const;
int getX() const;
int getY() const;
AguiMouseEventArgs();
AguiMouseEventArgs(const AguiPoint &position,
int mouseWheelChange, AguiMouseButtonEnum button);
};
class AguiKeyEventArgs {
AguiKeyEnum key;
AguiKeyModifierEnum modKey;
bool isAlt;
bool isControl;
bool isShift;
public:
bool getAlt() const;
bool getControl() const;
bool getShift() const;
AguiKeyEnum getKey() const;
AguiKeyModifierEnum getModifierKeyFlags() const;
AguiKeyEventArgs();
AguiKeyEventArgs(AguiKeyEnum key, AguiKeyModifierEnum modKey);
};
class AguiControlEventArgs {
AguiWidgetBase* control;
public:
AguiWidgetBase* getControl() const;
AguiControlEventArgs();
AguiControlEventArgs(AguiWidgetBase* control);
};
我已经仔细检查并定义了所有这些。
我的Agui活动在这里:
template <typename T>
class AguiEvent {
void (*onEvent)(T arg);
public:
void setHandler(void (*eventProc)(T arg));
void removeHandler();
void call(T arg);
AguiEvent();
};
其定义:
template <typename T>
void AguiEvent<T>::setHandler( void (*eventProc)(T arg) )
{
onEvent = eventProc;
}
template <typename T>
void AguiEvent<T>::removeHandler()
{
onEvent = 0;
}
template <typename T>
void AguiEvent<T>::call(T arg)
{
if(onEvent)
onEvent(arg);
}
template <typename T>
AguiEvent<T>::AguiEvent()
{
onEvent = 0;
}
它的使用方式如下:
AguiEvent<AguiEmptyEventArgs> eventThemeChanged;
AguiEvent<AguiControlEventArgs> eventChildControlAdded;
AguiEvent<AguiControlEventArgs> eventChildControlRemoved;
例如:
void AguiWidgetBase::addChildControl( AguiWidgetBase *control )
{
onAddChildControl(control);
eventChildControlAdded.call(AguiControlEventArgs(control));
}
由于
答案 0 :(得分:4)
您需要在头文件中定义模板化的类函数,不能将它们放在单独的.cpp文件中(甚至不要考虑使用支持不良的export
关键字来执行此操作)。
原因是因为编译器每次实例化时都需要模板的源代码:它必须为每个模板实例化生成单独的代码。链接器足够智能,以确保在最终可执行文件中只使用每个实例化的一个副本,即使相同的模板在不同的翻译单元中多次实例化。