实现 IUnknown、未解析的外部符号
我正在尝试创建一个实现 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 长 __stdcall Vmr9Presenter::QueryInterface(struct _GUID const &,void * *)" (?QueryInterface@Vmr9Presenter@@UAGJABU_GUID@@PAPAX@Z) Vmr9Presenter.obj VmrPresenter
这让我相信有些东西没有被拉进去。关于如何消除此错误有什么建议吗?
I am trying to create a class that implements the IUnknown
interface. I have the following code in the header file:
#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);
};
I have included the relevant uuid.lib
and several others. However, when I attempt to compile, I get the error:
Error 2 error LNK2001: unresolved external symbol "public: virtual
long __stdcall Vmr9Presenter::QueryInterface(struct _GUID const &,void
* *)" (?QueryInterface@Vmr9Presenter@@UAGJABU_GUID@@PAPAX@Z) Vmr9Presenter.obj VmrPresenter
This leads me to believe that something isn't getting pulled in. Any suggestions on how to get rid of this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有 I* 接口都只是接口定义。接口是 C++ 术语中的纯虚拟基类。
当您说:
您是在说“Vmr9Presenter 类实现了这些接口”。您还说“Vmr9Presenter 类派生自两个名为 IVMRImagePresenter9 和 IVMRSurfaceAllocator9 的纯虚拟基类。按照惯例,所有接口均派生自名为 IUnknown 的纯虚拟基类。
这意味着您需要实现纯虚拟基类中的所有方法因此,您需要实现 IVMRImagePresenter9 和 IVMRSurfaceAllocator9 上的所有方法,您还需要实现其基类上的所有方法,包括 IUnknown
有 3 个方法:AddRef、Release 和 IUnknown。您报告的错误表明链接器无法找到名为 Vmr9Presenter::QueryInterface 的函数。
您需要将这样的函数添加到您的类中,通常
QI 实现应该如下所示:
All the I* interfaces are just that - interface definitions. An interface is a pure virtual base class in C++ terms.
When you say:
you're saying "the Vmr9Presenter class implements these interfaces". You're also saying "The Vmr9Presenter class derives from two pure virtual base classes named IVMRImagePresenter9 and IVMRSurfaceAllocator9. By convention all interfaces derive from a pure virtual base class called IUnknown.
This means that you need to implement all the methods in the pure virtual base classes in your object. So you need to implement all the methods on IVMRImagePresenter9 and IVMRSurfaceAllocator9. You ALSO need to implement all the methods on their base classes, including IUnknown.
IUnknown has 3 methods: AddRef, Release and QueryInterface. The error you're reporting says that the linker was unable to find a function named Vmr9Presenter::QueryInterface.
You need to add such a function to your class, once you do that it should work.
Typically a QI implementation looks like:
IVMRImagePresenter9、IVMRurfaceAllocator9 是否已实现 IUnknown?也许您需要:
我猜您可能还需要根据 IUnknown 的文档。
Do either of IVMRImagePresenter9, IVMRSurfaceAllocator9 already implement IUnknown? Maybe you need:
I would guess you may also need to implement AddRef() and Release() too according to the docs for IUnknown.