views:

136

answers:

4

I'm writing some code to handle video input from some cameras using DirectShow, so I have to implement ISampleGrabberCB.

My class that implements the interface compiles okay, but when I try to instantiate it the compiler raises "error C2259: 'SampleGrabberCB' : cannot instantiate abstract class".

Here is the interface I'm implementing:

interface ISampleGrabberCB : public IUnknown {
    virtual STDMETHODIMP SampleCB( double SampleTime, IMediaSample *pSample ) = 0;
    virtual STDMETHODIMP BufferCB( double SampleTime, BYTE *pBuffer, long BufferLen ) = 0;
};

Here is my SampleGrabberCB header:

#pragma once

#include "stdafx.h"

class SampleGrabberCB : public ISampleGrabberCB {
private:

    int                 _refCount;
    DShowCaptureDevice* _parent;

public:
//  SampleGrabberCB();
    SampleGrabberCB(DShowCaptureDevice* parent);
    ~SampleGrabberCB();

    virtual STDMETHODIMP BufferCB(double sampleTime, BYTE* pBuffer, long bufferLen);
    virtual STDMETHODIMP SampleCB(double sampleTime, IMediaSample * pSample, long bufferLen);

#pragma region IUnknown

    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) {

        if( riid == IID_ISampleGrabberCB || riid == IID_IUnknown ) {

            *ppv = (void *) static_cast<ISampleGrabberCB*> ( this );
            return NOERROR;
        }

        return E_NOINTERFACE;

    }

    virtual ULONG STDMETHODCALLTYPE AddRef() {
        return ++_refCount;
    }

    virtual ULONG STDMETHODCALLTYPE Release() {
        int n = --_refCount;
        if (n &lt;= 0) {
            delete this;
        }
        return n;
    }

#pragma endregion
};

and this is the implementation SampleGrabberCB.cpp:

#include "StdAfx.h"
#include "SampleGrabberCB.h"

//SampleGrabberCB::SampleGrabberCB() {
//  _parent = NULL;
//}

SampleGrabberCB::SampleGrabberCB(DShowCaptureDevice* parent) {

    _parent = parent;
}

SampleGrabberCB::~SampleGrabberCB() {
}

STDMETHODIMP SampleGrabberCB::BufferCB(double sampleTime, BYTE *pBuffer, long bufferLen) {
    // dummy value for now
    return -50;
}

STDMETHODIMP SampleGrabberCB::SampleCB(double sampleTime, IMediaSample *pSample, long bufferLen) {
    // dummy value for now
    return 100;
}

Here is how I'm using it:

SampleGrabberCB* callback = new SampleGrabberCB(device);

Any ideas? Thanks!

+6  A: 

SampleCB as declared in the interface doesn't have the third parameter (bufferLen) that is present in the SampleGrabberCB class.

Will A
*headdesk* Thank you!I'm annoyed that the IDE didn't list the methods that weren't matching the interface (like it does for C# or Java). Is there any way to enable this, or is it inherent in C++'s compiler-complexity?
David
No problem. I'm not a C++ developer (well, I've not used VC++ for over seven years), so I'm afraid I can't comment on getting this information - someone else may well be able to tell you - I'd have certainly thought this would be outputtable.
Will A
+2  A: 

Your SampleCB method doesn't match the method in the base (abstract) class.

STDMETHODIMP SampleGrabberCB::SampleCB(double sampleTime, IMediaSample *pSample, long bufferLen)

versus

virtual STDMETHODIMP SampleCB( double SampleTime, IMediaSample *pSample ) = 0; 
Starkey
+1  A: 

The signature of your virtual STDMETHODIMP SampleCB(double sampleTime, IMediaSample * pSample, long bufferLen) method doesn't matcht that of the pure virtual mether in the Interface - you need to drop the bufferlen argument.

Stephen C. Steel
+1  A: 

It looks like the method signature is different in SampleCB method. On case has 2 parameters and the other 3 parameters.

Jim Ecker