c++ - 非模板类的模板子类 - 未解析的外部

标签 c++ templates

<分区>

Possible Duplicate:
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++ 的新手,所以我希望我做错了一些愚蠢的事情。

最佳答案

您可以通过将您的定义移动到头文件中来解决您的问题。虽然也有其他解决方案,但我会让其他人更好地解释它们——请参阅这些链接:

FAQ

Another Answer

关于c++ - 非模板类的模板子类 - 未解析的外部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168320/

相关文章:

c++ - 苹果操作系统 X : Where should I store save games for a game delivered as a bundle?

c++ - 可变含义逻辑常量是否存在任何强制机制?

c++ - 乘以可变数量的参数

C++ 在模板声明中使用默认值

c++ - 部分模板特化 c++ 中不完整类型的无效使用

c++ - 字符串类 C++

c++ - 使用 namespace 别名优于预处理器的好处

C++ istreambuf_iterator 模板参数

jquery - 我想创建一个带虚线的有序列表,指示文件的大小。怎么办?

c++ - 在不知道声明签名的情况下将任意函数对象存储到类成员容器中