1

重複の可能性:
C++ テンプレート、リンク エラー
テンプレート演算子オーバーロード関数の未定義シンボル

だから私は抽象ボタンクラスを持っています:

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 ++に比較的慣れていないので、間違っている愚かなことがあるかもしれないと思っています。

4

1 に答える 1

0

定義をヘッダー ファイルに移動することで、問題を解決できます。他にも解決策がありますが、他の人にもっとよく説明してもらいます-これらのリンクを参照してください。

よくある質問

別の答え

于 2013-01-05T03:53:16.977 に答える