Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 非模板类的模板子类-未解析的外部_C++_Templates - Fatal编程技术网

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

C++ 非模板类的模板子类-未解析的外部,c++,templates,C++,Templates,可能重复: 所以我有一个抽象的按钮类: 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 impleme

可能重复:

所以我有一个抽象的按钮类:

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(); 
};
模板
类递增按钮:公共按钮
{
公众:
T&控制值;
T增量;
T minVal,maxVal;
递增按钮(T&controlValue):controlValue(controlValue){}
无效点击(int-mouseX,int-mouseY);
无效抽取();
};
被重写的方法如下所示:

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
}
模板
void IncrementButton::单击(int-mouseX,int-mouseY)
{
//用控制值做某事
}
模板
void IncrementButton::draw()
{
//使用controlValue的值
}
但这不会编译。它给了我一个错误:

Error   1   error LNK2001: unresolved external symbol "public: virtual void __thiscall IncrementButton<float>::click(int,int)" (?click@?$IncrementButton@M@@UAEXHH@Z)
错误1错误LNK2001:未解析的外部符号“public:virtual void\uu thiscall IncrementButton::click(int,int)”(?click@$IncrementButton@M@@UAEXHH@Z)
…draw()也是一样


有什么想法吗?我对C++比较陌生,所以我希望我可能做了一些愚蠢的事情。< /P> < P>你可以通过把你的定义移到头文件中来解决问题。虽然还有其他解决方案,但我会让其他人更好地解释它们——请参见以下链接:


头文件中是否有重写的方法?还是cpp文件?如果它们在cpp文件中,则需要在头文件中:我想您需要阅读以下内容:。@JaredC,这很有效!谢谢。另一种解决方案(这里链接的答案实际上没有解释)是在定义模板函数的.cpp文件中使用显式实例化。我认为提到这一点很重要,尽管将所有定义放在标题中通常是首选解决方案。@jogojapan Oli在上面的链接中提到了这一点,但我指向了更一般的常见问题解答主题。谢谢你指出这一点。