简体   繁体   English

为什么我会收到这些链接器错误?

[英]Why am I getting these linker errors?

I'm getting the following linker errors: 我收到以下链接器错误:

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

I have AguiEventArgs.h which has this: 我有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);
};

I have double checked and defined all of these. 我仔细检查并定义了所有这些。

My Agui event is here: 我的Agui活动在这里:

template <typename T>
class AguiEvent {
void (*onEvent)(T arg);
public:
void setHandler(void (*eventProc)(T arg));
void removeHandler();
void call(T arg);
AguiEvent();
};

its defined: 其定义:

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;
}

it is used like this: 它像这样使用:

 AguiEvent<AguiEmptyEventArgs> eventThemeChanged;
    AguiEvent<AguiControlEventArgs> eventChildControlAdded;
    AguiEvent<AguiControlEventArgs> eventChildControlRemoved;

ex: 例如:

void AguiWidgetBase::addChildControl( AguiWidgetBase *control )
{
    onAddChildControl(control);
    eventChildControlAdded.call(AguiControlEventArgs(control));

}

Thanks 谢谢

You need to define templated class functions in your header file, you cannot put them in a separate .cpp file (don't even think about using the poorly supported export keyword to do that). 您需要在头文件中定义模板化的类函数,不能将它们放在单独的.cpp文件中(甚至不要考虑使用支持不良的export关键字来执行此操作)。

The reason for this is because the compiler needs the source code for the template every time it's instantiated: it has to generate separate code for each template instantiation. 原因是因为编译器每次实例化时都需要模板的源代码:它必须为每个模板实例化生成单独的代码。 The linker is smart enough to make sure that only one copy of each instantiation gets used in the final executable, even if the same template gets instantiated multiple times across different translation units. 链接器足够智能,以确保在最终可执行文件中只使用每个实例化的一个副本,即使相同的模板在不同的转换单元中多次实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM