为什么VC2008认为这个类是抽象的?

时间:2010-08-30 17:19:45

标签: c++ abstract-class directshow

我正在编写一些代码来处理使用DirectShow的某些摄像头的视频输入,所以我必须实现ISampleGrabberCB。

我实现接口的类编译好了,但是当我尝试实例化它时,编译器会引发“error C2259: 'SampleGrabberCB' : cannot instantiate abstract class”。

这是我正在实施的界面:

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

这是我的SampleGrabberCB标题:

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

这是实现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;
}

以下是我如何使用它:

SampleGrabberCB* callback = new SampleGrabberCB(device);

有什么想法吗?谢谢!

4 个答案:

答案 0 :(得分:6)

接口中声明的

SampleCB没有bufferLen类中存在的第三个参数(SampleGrabberCB)。

答案 1 :(得分:2)

您的SampleCB方法与基础(抽象)类中的方法不匹配。

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

virtual STDMETHODIMP SampleCB( double SampleTime, IMediaSample *pSample ) = 0; 

答案 2 :(得分:1)

虚拟STDMETHODIMP SampleCB(double sampleTime,IMediaSample * pSample,long bufferLen)方法的签名与接口中纯虚拟mether的签名不匹配 - 您需要删除bufferlen参数。

答案 3 :(得分:1)

SampleCB方法中的方法签名看起来不同。案例有2个参数,其他3个参数。