Pregunta

He definido la siguiente CComPtr objeto y método en mi clase:

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().

En el constructor de mi clase, me inicializar mRawPdu a 0 a través de la lista initialisor. El método utilizado getRawPdu() evaluación perezosa si <=> aún no se ha inicializado.

Al compilar el código, consigo los errores siguientes:

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
        ]

Cualquier sugerencia en cuanto a lo que podría ser la causa de esto?

¿Fue útil?

Solución

En base al error propuesta por el compilador parece que no se puede inferir una conversión entre IRawPdu y IUnknown.

¿Realmente hereda de IUnknown? Si es así, entonces es posiblemente un problema incluir el pedido. ¿Puede dar una visión más clara la jerarquía de IRawPdu

Otros consejos

No deje pasar CComPtr <> 's alrededor ya que no hay necesidad, simplemente devuelve el puntero a la interfaz. Por ejemplo:

  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().

Así que, cuando llega el momento de utilizarlo:

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

Pero, mejor sería:

  CComPtr<IRawPdu> spRawPdu = class->getRawPdu();
  // the ctor of the local CComPtr<> calls AddRef() (and automagically Release's when done)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top