c ++模板纯抽象类

时间:2016-11-27 02:24:54

标签: c++ templates pure-virtual

当我构建模板类时,我可以使该类成为纯抽象类吗?

template <class Block>
class Filter
{
public:
    Filter();
    void add(Frame* frame);
    Block* get();
private:
    Block* mBlock;
};

template <class Block> Filter<Block>::Filter<Block>()
{ }

template <class Block> void Filter<Block>::add(Frame* frame)
{
    mBlock = new Block(
                frame->getSampleRate(),
                0xFFFFFFFF,
                frame->getBlockSize(),
                );
}

template <class Block> Block* Filter<Block>::get()
{ return mBlock; }

Block类是一个纯虚拟类,是传递给模板的基类。

class Block
{
public:
    explicit Block(DWORD dwSampleRate, DWORD dwMicrophoneIndex, DWORD dwBlockSize);
    ~Block();

    DWORD getSampleRate();
    DWORD getMicrophoneIndex();
    DWORD getBlockSize();

    virtual void process() = 0;
private:
    DWORD mdwSampleRate;
    DWORD mdwMicrophoneIndex;
    DWORD mdwBlockSize;
};

实现者将从Block创建一个派生类。

class FooBlock : public Block
{
public:
    FooBlock(DWORD dwSampleRate, DWORD dwMicrophoneIndex, DWORD dwBlockSize);
    void process();
};

实施为:

FooBlock::FooBlock(DWORD dwSampleRate, DWORD dwMicrophoneIndex, DWORD dwBlockSize)
: Block(dwSampleRate, dwMicrophoneIndex, dwBlockSize)
{ }

void FooBlock::process()
{ }

调用过滤器

Filter<FooBlock>* filter = new Filter<FooBlock>();

产生消息:

  

LNK2019:未解析的外部符号“public:__ cdecl Filter :: Filter(void)”(?? 0?$ Filter @ VBlock @@@@ QEAA @ XZ)   在函数“public:__ cdecl MainWindow :: MainWindow(class。)中引用   QWidget *)“(?? 0MainWindow @@ QEAA @ PEAVQWidget @@@ Z)

我可以创建需要基类的模板吗?或者我是否需要找到不同的技术来实现它?

0 个答案:

没有答案