1

私は古いc++コードを持っており、コンパイルしてc ++ builder 5にコンパイルしています。しかし今、このコードをc ++ builder 2009に更新/移行する必要があります。したがって、いくつかの問題があります。

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非const関数_fastcall TStrings :: GetCount()がconstオブジェクトに対して呼び出されました」。

では、どうすればそれを解決(回避)できますか?

4

1 に答える 1

3

TStringListの正確な定義を提供した場合に役立ちますが、タイプ名TStringのテンプレート化された配列であると想定します。

回避策は、次のようにconstをキャストすることです。

lngIndice = (const_cast<TStringList*>(pList))->Count;

もちろん、それはまさにそれです-回避策であり、代わりにTString自体にconst-correctアクセス関数を提供することを検討することをお勧めします

于 2012-07-12T13:55:46.723 に答える