我想實作一個有多個孩子的基類。
基類
template <typename T>
class Trigger
{
protected:
T threshold;
Trigger(T a_threshold) : threshold(std::pow(10, a_threshold / 20)){};
public:
void setThreshold(T a_threshold)
{
threshold = std::pow(10, a_threshold / 20);
}
virtual bool operator()(const T &first, const T &second) const;
virtual ~Trigger() = default;
};
派生類
template <typename T>
class ZeroCrossingRisingTrigger : public Trigger<T>
{
public:
ZeroCrossingRisingTrigger(void) : Trigger<T>(0.){};
bool operator()(const T &first, const T &second) const override
{
return (first <= 0) & (second > 0);
}
};
在主檔案中的用法
#include "Trigger.hpp"
int main([[maybe_unused]] int argc, [[maybe_unused]] char const *argv[])
{
Trigger::ZeroCrossingRisingTrigger<double> trig;
return 0;
}
但是當我嘗試編譯它時出現以下錯誤:
(...): 對`Trigger::Trigger::operator()(double const&, double const&) const'的未定義參考
我不明白為什么會出現此錯誤,因為我完全按照錯誤訊息中的說明實作了運算子。
uj5u.com熱心網友回復:
您還沒有定義的實作operator()了Trigger<T>。一種選擇是Trigger通過使運算子成員函式成為純虛函式來創建抽象基類:
virtual bool operator()(const T &first, const T &second) const = 0;
或者,您可以提供一個空的實作。
附帶說明一下,在ZeroCrossingRisingTrigger您的建構式中,您將0.0作為基類建構式的引數傳遞。這種提示不需要ZeroCrossingRisingTrigger自己模板化,除非您想0.0從外部控制文字的型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/353808.html
