我在班上定义了以下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().
在我的类的构造函数中,我通过initialisor列表将mRawPdu
初始化为0。如果getRawPdu()
尚未初始化,则mRawPdu
方法使用延迟评估。
编译代码时,我收到以下错误:
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
]
有关可能导致此问题的任何建议吗?
答案 0 :(得分:2)
根据编译器给出的错误,它似乎无法推断IRawPdu和IUnknown之间的转换。
它是否真的从IUnknown继承?如果是这样,那么可能是包含订购问题。您能否更深入地了解IRawPdu的层次结构
答案 1 :(得分:0)
不要传递CComPtr&lt;&gt;,因为没有必要,只需返回指向接口的指针即可。例如:
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)