【问题标题】:Compilation errors through incorrect use of CComPtr objects错误使用 CComPtr 对象导致的编译错误
【发布时间】:2010-11-07 05:07:24
【问题描述】:

我在我的类中定义了以下CComPtr 对象和方法:

private:

    CComPtr<IRawPdu>& getRawPdu();
    // Returns the RawPdu interface pointer from the mRawPdu data member.
    // mRawPdu is initialized, if necessary.

    CComPtr<IRawPdu> mRawPdu;
    // Initialized to 0 in the ctor.  Uses lazy evaluation via getRawPdu().

在我的类的构造函数中,我通过初始化列表将mRawPdu 初始化为0。如果 mRawPdu 尚未初始化,则 @​​987654324@ 方法使用惰性求值。

编译代码时出现以下错误:

Compiling...
topport.cpp
C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcomcli.h(295) : error C2664: 'ATL::AtlComPtrAssign' : cannot convert parameter 2 from 'const ATL::CComPtr<T>' to 'IUnknown *'
        with
        [
            T=IRawPdu
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
        C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcomcli.h(292) : while compiling class template member function 'IRawPdu *ATL::CComPtr<T>::operator =(const ATL::CComPtr<T> &) throw()'
        with
        [
            T=IRawPdu
        ]
        sessionutilities.h(186) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
        with
        [
            T=IRawPdu
        ]
topglobals.cpp
C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcomcli.h(295) : error C2664: 'ATL::AtlComPtrAssign' : cannot convert parameter 2 from 'const ATL::CComPtr<T>' to 'IUnknown *'
        with
        [
            T=IRawPdu
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
        C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\atlcomcli.h(292) : while compiling class template member function 'IRawPdu *ATL::CComPtr<T>::operator =(const ATL::CComPtr<T> &) throw()'
        with
        [
            T=IRawPdu
        ]
        sessionutilities.h(186) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
        with
        [
            T=IRawPdu
        ]

关于可能导致此问题的任何建议?

【问题讨论】:

    标签: c++ com visual-studio-2005 atl


    【解决方案1】:

    根据编译器给出的错误,它似乎无法推断出 IRawPdu 和 IUnknown 之间的转换。

    它实际上是从 IUnknown 继承的吗?如果是这样,则可能是包含订购问题。您能否更深入地了解 IRawPdu 的层次结构

    【讨论】:

    • IRawPdu 继承自 IDispatch,而 IDispatch 又继承自 IUnknown。奇怪的是这工作正常,但后来我将 mRawPdu 对象移到另一个类中,只有现在它因这些错误而失败。所以也许这是一个包含排序问题,但我不确定如何隔离它。
    • 我刚刚想通了——你的“包括订购问题”评论就是线索。问题是许多其他 .cpp 文件包含我的头文件和 CComPtr 定义,但 IRawPdu 声明仅包含在我使用它的 .cpp 文件中。我将定义接口的 MIDL 生成的头文件移动到 stdafx.h 预编译头文件中,它似乎解决了问题。感谢您的建议!
    【解决方案2】:

    不要传递 CComPtr ,因为没有必要,只需返回指向接口的指针。例如:

      IRawPdu* getRawPdu() { return mRawPdu; }   // Does not add to the reference count
    
      HRESULT get_RawPdu(IRawPdu** ppPdu)   // Returns RawPdu, but add ref's it.
      {
         return mRawPdu.CopyTo(ppPdu);
      }
    
      CComPtr<IRawPdu> mRawPdu;
      // Initialized to 0 in the ctor.  Uses lazy evaluation via getRawPdu().
    

    所以,到了使用它的时候:

      IRawPdu* pTempRawPdu  = class->getRawPdu();
      // use pTempRawPdu in a temporary manner (since it's not add reffed)
    

    但是,更好的是:

      CComPtr<IRawPdu> spRawPdu = class->getRawPdu();
      // the ctor of the local CComPtr<> calls AddRef() (and automagically Release's when done)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2013-02-01
      • 2015-09-12
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多