error C2664: cannot convert argument 2 from 'const ATL::CAdapt<ATL::CComPtr<IZipFileEntry>> *' to 'ATL::CAdapt<ATL::CComPtr<IZipFileEntry>> *'

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

Pergunta

I'm migrating VS2008(VC++) code to VS2013. I get the below error:

error C2664: 'HRESULT _CopyItfFromAdaptItf<IZipFileEntry>::copy(T **,ATL::CAdapt<ATL::CComPtr<T>> *)' : cannot convert argument 2 from 'const ATL::CAdapt<ATL::CComPtr<IZipFileEntry>> *' to 'ATL::CAdapt<ATL::CComPtr<IZipFileEntry>> *'

The error appears in the Visual studio SDK File, how is it possible?. I'm not able to fix this error. Please find below the output and error line below.

Thanks a lot for any help!!

Output:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include\atlcom.h(5818): error C2664: 'HRESULT _CopyItfFromAdaptItf<IZipFileEntry>::copy(T **,ATL::CAdapt<ATL::CComPtr<T>> *)' : cannot convert argument 2 from 'const ATL::CAdapt<ATL::CComPtr<IZipFileEntry>> *' to 'ATL::CAdapt<ATL::CComPtr<IZipFileEntry>> *'
      with
      [
          T=IZipFileEntry
      ]
      Conversion loses qualifiers
      C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include\atlcom.h(5803) : while compiling class template member function 'HRESULT ATL::ICollectionOnSTLImpl<IZipFileDir1,EntryList,IZipFileEntry *,_CopyItfFromAdaptItf<IZipFileEntry>,EntryEnum>::get_Item(long,ItemType *)'
      with
      [
          ItemType=IZipFileEntry *
      ]
      C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include\atlcom.h(5140) : see reference to class template instantiation 'ATL::ICollectionOnSTLImpl<IZipFileDir1,EntryList,IZipFileEntry *,_CopyItfFromAdaptItf<IZipFileEntry>,EntryEnum>' being compiled

Error Code:

if (iter != m_coll.end())
      hr = CopyItem::copy(pvar, &*iter); // Error C2664
return hr;

P.S: I'm not able to edit and save this atlcom.h file. I get access to the path is denied error message. Is this because its a SDK File?

Some more code definitions added:

struct _CopyItfFromAdaptItf {
    static HRESULT copy(T** p1, CAdapt< CComPtr<T> >* p2) {
    if( *p1 = p2->m_T ) return (*p1)->AddRef(), S_OK;
    return E_POINTER;
}


IZipFileEntry : public IDispatch
{
    public:
        virtual /* [helpstring][id][propget] */ HRESULT STDMETHODCALLTYPE get_name( 
        /* [retval][out] */ BSTR *pVal) = 0;

        virtual /* [helpstring][id][propget] */ HRESULT STDMETHODCALLTYPE get_lastModified( 
        /* [retval][out] */ DATE *pVal) = 0;

        virtual /* [helpstring][id][propget] */ HRESULT STDMETHODCALLTYPE get_fileSize( 
        /* [retval][out] */ long *pVal) = 0;

        virtual /* [helpstring][id][propget] */ HRESULT STDMETHODCALLTYPE get_compressedSize( 
        /* [retval][out] */ long *pVal) = 0;

    };


typedef ICollectionOnSTLImpl<IZipFileDir1, EntryList, IZipFileEntry*, _CopyItfFromAdaptItf<IZipFileEntry>, EntryEnum> EntryCollection;

Definition of ICollectionOnSTLImpl

class ICollectionOnSTLImpl : 
    public T
{
public:
        STDMETHOD(get_Count)(_Out_ long* pcount)
        {
        if (pcount == NULL)
            return E_POINTER;
        ATLASSUME(m_coll.size()<=LONG_MAX);

        *pcount = (long)m_coll.size();

        return S_OK;
    }
    STDMETHOD(get_Item)(
        _In_ long Index, 
        _Out_ ItemType* pvar)
    {
        //Index is 1-based
        if (pvar == NULL)
            return E_POINTER;
        if (Index < 1)
            return E_INVALIDARG;
        HRESULT hr = E_FAIL;
        Index--;
        CollType::const_iterator iter = m_coll.begin();
        while (iter != m_coll.end() && Index > 0)
        {
            iter++;
            Index--;
        }
        if (iter != m_coll.end())
            hr = CopyItem::copy(pvar, &*iter);
        return hr;
    }
    STDMETHOD(get__NewEnum)(_Outptr_ IUnknown** ppUnk)
    {
        if (ppUnk == NULL)
            return E_POINTER;
        *ppUnk = NULL;
        HRESULT hRes = S_OK;
        CComObject<EnumType>* p;
        hRes = CComObject<EnumType>::CreateInstance(&p);
        if (SUCCEEDED(hRes))
        {
            hRes = p->Init(this, m_coll);
            if (hRes == S_OK)
                hRes = p->QueryInterface(__uuidof(IUnknown), (void**)ppUnk);
        }
        if (hRes != S_OK)
            delete p;
        return hRes;
    }
    CollType m_coll;
};
Foi útil?

Solução

It's hard to say for sure without all the code but it looks like I've reproduced the problem with the following code:

template<class T>
struct _CopyItfFromAdaptItf {
    static HRESULT copy(T** p1, CAdapt< CComPtr<T> >* p2) {
        if( *p1 = p2->m_T ) return (*p1)->AddRef(), S_OK;
        return E_POINTER;
    }
};

IDispatch** disp;
const CAdapt<CComPtr<IDispatch>> adapt; // note const here
_CopyItfFromAdaptItf<IDispatch>::copy( disp, &adapt ); //error C2664 here

The compiler output is

File(Line) : error C2664: '_CopyItfFromAdaptItf<T>::copy' :
  cannot convert parameter 2 from 'const ATL::CAdapt<T> *' to 'ATL::CAdapt<T> *'
    with
    [
      T=IDispatch
    ]
    and
    [
      T=ATL::CComPtr<IDispatch>
    ]
    and
    [
      T=ATL::CComPtr<IDispatch>
    ]
    Conversion loses qualifiers

which pretty much matches the output you cited.

The problem is that _CopyItfFromAdaptItf::copy() wants a pointer to non-const but a pointer to const is passed. Code that passes a pointer to const is inside atlcom.h which belongs to SDK headers and so you'd better not change it.

Also if _CopyItfFromAdaptItf::copy() signature is changed to accept a pointer to const:

static HRESULT copy(T** p1, const CAdapt< CComPtr<T> >* p2) // note addition of const

it still compiles just fine. _CopyItfFromAdaptItf is nowhere in the ATL headers so I guess it's in your code and you can change it.

So it looks like the solution is to simple change _CopyItfFromAdaptItf::copy() to accept a pointer to const and the problem will go away.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top