可能重复:
C++ template, linking error
Undefined symbol on a template operator overloading function
所以我有一个抽象的Button类:
class Button
{
public:
int x, y, width, height;
std::string label, text;
bool checkForClick(int mouseX, int mouseY);
virtual void click(int mouseX, int mouseY) =0;
virtual void draw(); //has a default implementation which can be overridden
};
和我想成为模板类的子类:
template <typename T>
class IncrementButton : public Button
{
public:
T& controlValue;
T increment;
T minVal, maxVal;
IncrementButton(T& controlValue) : controlValue(controlValue) {}
void click(int mouseX, int mouseY);
void draw();
};
重写的方法如下:
template <typename T>
void IncrementButton<T>::click(int mouseX, int mouseY)
{
//do something with controlValue
}
template <typename T>
void IncrementButton<T>::draw()
{
//use value of controlValue
}
但这不会编译。它给了我错误:
Error 1 error LNK2001: unresolved external symbol "public: virtual void __thiscall IncrementButton<float>::click(int,int)" (?click@?$IncrementButton@M@@UAEXHH@Z)
...和draw()相同的东西
有什么想法吗?我对C ++比较陌生,所以我希望也许有些傻事我做错了。