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

发布于 2024-09-16 06:29:56 字数 2231 浏览 12 评论 0原文

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

我实现该接口的类可以正常编译,但是当我尝试实例化它时,编译器会引发“错误 C2259:'SampleGrabberCB':无法实例化抽象类”。

这是我正在实现的接口:

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

有什么想法吗?谢谢!

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 <= 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月隐月明月朦胧 2024-09-23 06:29:56

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

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

池予 2024-09-23 06:29:56

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

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

相对

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

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; 
泪眸﹌ 2024-09-23 06:29:56

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

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.

瞎闹 2024-09-23 06:29:56

SampleCB 方法中的方法签名似乎不同。 On case 有 2 个参数,其他 3 个参数。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文