0

When I build a template class can I make the class a pure abstract class?

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; }

the Block class is a pure virtual class and is the base class for what is being passed into the template.

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;
};

The implementer will create a derived class from Block.

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

implemented as:

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

void FooBlock::process()
{ }

To invoke the filter

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

which yields the message:

LNK2019: unresolved external symbol "public: __cdecl Filter::Filter(void)" (??0?$Filter@VBlock@@@@QEAA@XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)

Can I create a template that requires a base class? Or do I need find a different technique to implement this?

Steephen
  • 14,645
  • 7
  • 40
  • 47
Nefarious
  • 458
  • 6
  • 21
  • 1
    I'll bet that `template Filter::Filter() { }` is not defined in a header file, right? The header file contains only the template class declaration, right? – Sam Varshavchik Nov 27 '16 at 02:33
  • You describe `Block` as "a pure virtual class" but I don't know what a virtual class means in C++. `Block` isn't a pure *abstract* class as it has data members. – Jack Deeth Nov 27 '16 at 02:34
  • 1
    `template Filter::Filter() { }` This shouldn't compile. The correct syntax is `template Filter::Filter() { }`. It appears that the code you show is different from the code you actually build. – Igor Tandetnik Nov 27 '16 at 02:38
  • Yes, the header file just defines the class, the implementation is in a cpp file. – Nefarious Nov 27 '16 at 03:01
  • @Nefarious see my duplicate suggestion – Danh Nov 27 '16 at 03:40

0 Answers0