实现IUnknown,未解析的外部符号
-
06-07-2019 - |
题
我正在尝试创建一个实现 IUnknown
接口的类。我在头文件中有以下代码:
#pragma once
#include "stdafx.h"
#include "Unknwn.h"
class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9
{
public:
Vmr9Presenter(void);
HRESULT Initialize(void);
~Vmr9Presenter(void);
STDMETHODIMP QueryInterface(const IID &riid, void **ppvObject);
};
我已经包含了相关的 uuid.lib
和其他几个。但是,当我尝试编译时,我收到错误:
错误2错误LNK2001:未解析的外部符号" public:virtual long __stdcall Vmr9Presenter :: QueryInterface(struct _GUID const&,void * *)“ (?QueryInterface @ Vmr9Presenter @@ UAGJABU_GUID @@ PAPAX @ Z)Vmr9Presenter.obj VmrPresenter
这让我相信某些东西没有被拉进去。有关如何摆脱这个错误的任何建议吗?
解决方案
所有I *接口都只是 - 接口定义。接口是C ++术语中的纯虚基类。
当你说:
class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9
你说的是“Vmr9Presenter类实现了这些接口”。您还说“Vmr9Presenter类派生自两个名为IVMRImagePresenter9和IVMRSurfaceAllocator9的纯虚拟基类。按照惯例,所有接口都派生自一个名为IUnknown的纯虚基类。
这意味着您需要在对象的纯虚基类中实现所有方法。因此,您需要在IVMRImagePresenter9和IVMRSurfaceAllocator9上实现所有方法。您还需要在他们的基类上实现所有方法,包括IUnknown。
IUnknown有3个方法:AddRef,Release和QueryInterface。您报告的错误表明链接器无法找到名为Vmr9Presenter :: QueryInterface的函数。
你需要在课堂上添加这样的功能,一旦你这样做,它应该可以工作。
通常,QI实现如下:
HRESULT IVmr9Presenter::QueryInterface(REFIID iid, PVOID *pvInterface)
{
if (pvInterface == NULL)
{
return E_POINTER;
}
*pvInterface = NULL;
if (iid == IID_IUnknown)
{
*pvInterface = static_cast<PVOID>(static_cast<IUnknown *>(this));
return S_OK;
}
if (iid == IID_IVMRSurfaceAllocator9)
{
*pvInterface = static_cast<PVOID>(static_cast<IVMRSurfaceAllocator9*>(this));
return S_OK;
}
:
else
{
return E_NOINTERFACE;
}
}
其他提示
IVMRImagePresenter9,IVMRSurfaceAllocator9中的任何一个是否已实现IUnknown?也许你需要:
class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9, IUnknown
我猜你也可能需要根据 docs 。