أخطاء ترجمة من خلال الاستخدام غير الصحيح للكائنات CComPtr

StackOverflow https://stackoverflow.com/questions/1067066

سؤال

ولقد عرف الكائن 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().

في منشئ صفي، I التهيئة mRawPdu 0 عبر قائمة initialisor. استخدام أسلوب getRawPdu() تقييم كسول إذا لم يتم بعد initialised 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
        ]

وأي اقتراحات بشأن ما يمكن أن يسبب هذا؟

هل كانت مفيدة؟

المحلول

واستنادا إلى خطأ معين من قبل المجمع يبدو أنه لا يمكن الاستدلال على التحويل بين IRawPdu وIUnknown.

هل ترث في الواقع من IUnknown؟ إذا كان الأمر كذلك ثم انها ربما التضمين الطلب القضية. يمكنك ان تعطي المزيد من التبصر في التسلسل الهرمي للIRawPdu

نصائح أخرى

ولا يمر 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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top