为const对象调用了非常量函数_fastcall TStrings::GetCount()
Non-const function _fastcall TStrings::GetCount() called for const object
我有一个旧的c++代码,经过布线并编译到c++构建器5中。但现在,我需要将此代码更新/迁移到c++builder2009。所以,我有一些问题:
int __fastcall TAllConversor::ListToStr(
const TStringList* pList,
AnsiString& strValue,
const long lngLimiteInferior,
const long lngLimiteSuperior) const
{
long lngIndice;
AnsiString strAux;
try
{
if (lngLimiteSuperior == 0)
lngIndice = pList->Count;
else
lngIndice = lngLimiteSuperior + lngLimiteInferior;
for (int i = lngLimiteInferior; i < lngIndice; i++)
{
strAux += pList->Strings[i] + ";";
}
strValue = strAux;
return 1;
}
catch(...)
{
return 0;
}
}
在第"lngIndice=pList->Count;"行,我得到了这个错误:"为常量对象调用了E2522 Non-const函数_fastcall TStrings::GetCount()"。
那么,我该如何解决(解决)它呢?
如果您提供TStringList的精确定义会有所帮助,但我只假设它是一个用于typename TString的模板化数组。
变通办法可能是去掉const,如:
lngIndice = (const_cast<TStringList*>(pList))->Count;
当然,这就是它的本质——一种变通方法,您可能希望在TString本身中提供一个常量正确的访问函数,而不是