将指针传递给派生类会产生编译器错误

时间:2017-05-20 08:14:19

标签: c++ com

我正在实施IDropTarget界面。在编译下面的代码时,编译器给出了错误"错误:函数调用' CoLockObjectExternal(CDropTarget *,int,int)'不匹配 ' CoLockObjectExternal(IUnknown *,int,int)'"

IDropTarget * 
txtenum_droptarget_register
(
    HWND    wnd
)
{
    CDropTarget *pDropTarget = new CDropTarget(wnd);


    /*
     *  acquire a strong lock
     */
    CoLockObjectExternal(pDropTarget, TRUE, FALSE);

    /*
     *  tell OLE that the window is a drop target
     */
    RegisterDragDrop(wnd, pDropTarget);

    return( pDropTarget );
}

班级定义:

/*
 *  This is our definition of a class which implements the IDropTarget interface
 */
class CDropTarget : public IDropTarget
{
public:
    /*
     *  IUnknown implementation
     */
    HRESULT __stdcall QueryInterface (REFIID iid, void ** ppvObject);
    ULONG   __stdcall AddRef (void);
    ULONG   __stdcall Release (void);

    /*
     *  IDropTarget implementation
     */
    HRESULT __stdcall DragEnter (IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);
    HRESULT __stdcall DragOver (DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);
    HRESULT __stdcall DragLeave (void);
    HRESULT __stdcall Drop (IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);

    /*
     *  Constructor
     */
    CDropTarget(HWND wnd);
    ~CDropTarget();

private:

    /*
     *  internal helper function
     */
    DWORD DropEffect(DWORD grfKeyState, POINTL pt, DWORD dwAllowed);
    bool  QueryDataObject(IDataObject *pDataObject);

    /*
     *  Private member variables
     */
    LONG    m_lRefCount;
    HWND    m_hWnd;
    bool    m_fAllowDrop;

    IDataObject *m_pDataObject;

};

在我的理解中CDropTarget实现了IDropTarget接口,而接口又是从(实现)IUnkown(接口)派生的,因此不需要进行类型转换。

1 个答案:

答案 0 :(得分:0)

为了实现拖放,我激活了C ++编译器(之前我使用的是C编译器)。我正在使用预编译的标头。在不更改此标头的源代码的情况下,我使用C ++编译器对其进行了编译。但是,在这个预编译的头文件中,找到了以下定义:

#define CINTERFACE

IDropTarget接口在oleidl.h中定义:

EXTERN_C const IID IID_IDropTarget;

#if defined(__cplusplus) && !defined(CINTERFACE)

 IDropTarget : public IUnknown
 {
     ....
 }
 #else  /* C style interface */

 interface IDropTarget
 {
     ....
 };

 #endif

所以即使我激活了C ++编译器(定义了__cplusplus),接口也被定义为结构而不是类(CINTERFACE也定义了)。