我将VS2008(VC ++)代码迁移到VS2013。我收到以下错误:
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>> *'
错误出现在Visual Studio SDK文件中,怎么可能?我无法解决此错误。请在下面找到输出和错误行。
非常感谢您的帮助!!
输出:
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
错误代码:
if (iter != m_coll.end())
hr = CopyItem::copy(pvar, &*iter); // Error C2664
return hr;
P.S:我无法编辑和保存此atlcom.h文件。我得到访问路径被拒绝的错误消息。这是因为它是一个SDK文件吗?
添加了更多代码定义:
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;
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;
};
答案 0 :(得分:2)
如果没有所有代码,很难肯定地说,但看起来我用以下代码重现了这个问题:
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
编译器输出
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
几乎与您引用的输出相匹配。
问题是_CopyItfFromAdaptItf::copy()
想要一个指向非const的指针,但是传递了一个指向const的指针。传递指向const的指针的代码位于atlcom.h里面,它属于SDK头文件,因此你最好不要更改它。
此外,如果更改_CopyItfFromAdaptItf::copy()
签名以接受指向const的指针:
static HRESULT copy(T** p1, const CAdapt< CComPtr<T> >* p2) // note addition of const
它仍然编译得很好。 _CopyItfFromAdaptItf
在ATL标题中没有任何地方,所以我猜它在您的代码中,您可以更改它。
所以看起来解决方案就是简单地改变_CopyItfFromAdaptItf::copy()
来接受指向const的指针,问题就会消失。