为什么VC2008认为这个类是抽象的?
我正在编写一些代码来使用 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 <= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
接口中声明的
SampleCB
没有SampleGrabberCB
类中存在的第三个参数 (bufferLen
)。SampleCB
as declared in the interface doesn't have the third parameter (bufferLen
) that is present in theSampleGrabberCB
class.您的 SampleCB 方法与基(抽象)类中的方法不匹配。
相对
Your SampleCB method doesn't match the method in the base (abstract) class.
versus
您的虚拟 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.
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.