简体   繁体   English

实现IUnknown,未解析的外部符号

[英]Implementing IUnknown, unresolved external symbol

I am trying to create a class that implements the IUnknown interface. 我正在尝试创建一个实现IUnknown接口的类。 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. 我已经包含了相关的uuid.lib和其他几个。 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 错误2错误LNK2001:未解析的外部符号“公共:虚拟长__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? 这使我相信某些东西没有被引入。有关如何消除此错误的任何建议?

All the I* interfaces are just that - interface definitions. 所有的I *接口都只是接口定义。 An interface is a pure virtual base class in C++ terms. 接口是使用C ++术语的纯虚拟基类。

When you say: 当你说:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9

you're saying "the Vmr9Presenter class implements these interfaces". 您是说“ Vmr9Presenter类实现了这些接口”。 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. 您还说的是:“ Vmr9Presenter类是从两个名为IVMRImagePresenter9和IVMRSurfaceAllocator9的纯虚拟基类派生的。按照惯例,所有接口都从称为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. 因此,您需要在IVMRImagePresenter9和IVMRSurfaceAllocator9上实现所有方法。 You ALSO need to implement all the methods on their base classes, including IUnknown. 您还需要在它们的基类上实现所有方法,包括IUnknown。

IUnknown has 3 methods: AddRef, Release and QueryInterface. IUnknown有3种方法:AddRef,Release和QueryInterface。 The error you're reporting says that the linker was unable to find a function named Vmr9Presenter::QueryInterface. 您报告的错误表明链接器无法找到名为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: 通常,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;
    }
}

Do either of IVMRImagePresenter9, IVMRSurfaceAllocator9 already implement IUnknown? IVMRImagePresenter9和IVMRSurfaceAllocator9是否已实现IUnknown? Maybe you need: 也许您需要:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9, IUnknown

I would guess you may also need to implement AddRef() and Release() too according to the docs for IUnknown. 我猜您可能还需要根据IUnknown的文档来实现AddRef()和Release()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM