Erros de compilação através do uso incorreto de objetos CComPtr
-
21-08-2019 - |
Pergunta
Eu tenho definido o objeto seguinte CComPtr
e método na minha classe:
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().
No construtor da minha classe, eu inicializar mRawPdu
a 0 através da lista initialisor. O método utilizado getRawPdu()
avaliação preguiçosa se mRawPdu
ainda tem de ser inicializado.
Ao compilar o código, eu recebo os seguintes erros:
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
]
Todas as sugestões sobre o que poderia estar causando isso?
Solução
Com base no erro dada pelo compilador parece que não se pode inferir uma conversão entre IRawPdu e IUnknown.
Será que ela realmente herdar de IUnknown? Se assim for, então é possivelmente um incluem problema de ordenação. Pode dar mais detalhes sobre a hierarquia das IRawPdu
Outras dicas
Não passe CComPtr <> 's em torno desde que não há necessidade, basta retornar o ponteiro para a interface. Por exemplo:
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().
Assim, quando chega a hora de usá-lo:
IRawPdu* pTempRawPdu = class->getRawPdu();
// use pTempRawPdu in a temporary manner (since it's not add reffed)
Mas, melhor seria:
CComPtr<IRawPdu> spRawPdu = class->getRawPdu();
// the ctor of the local CComPtr<> calls AddRef() (and automagically Release's when done)