GCC std::set question Topic is solved

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
Post Reply
gururamnath
Moderator
Moderator
Posts: 466
Joined: Sat Sep 18, 2004 2:49 am
Location: California, USA

GCC std::set question

Post by gururamnath » Tue Jan 01, 2008 1:02 am

Hello,
I'm trying to create a pascal like set class in C++. I have this code written and tested with Borland C++ builder (as well as VC++ 2005) and it works fine. When I compile the code in GCC, it is erroring out at line 72 with a error msg
"sysset.h expected `;' before "itr" ".

Can anyone please let me know what exactly is the problem ?

Thanks in advance.


Partial Code :

Code: Select all

    DelphiSet<T>& operator = ( const DelphiSet<T>& rhs )
    {
      if ( this != &rhs )
      {
        FSet.clear();
Ln 72 - std::set<T>::const_iterator itr;
        for (itr = rhs.FSet.begin(); itr != rhs.FSet.end(); ++itr)
        {
          FSet.insert(*itr);
        }
      }
      return *this;
    }
Full code :

Code: Select all

/*
Authors : Guru Kathiresan - [email protected] ,
          FreePascal Team - http://www.freepascal.org

License : wxWidgets License

*/

#ifndef SysSetH
#define SysSetH

#include <set>
#include <iterator>

template <class T>
class DelphiSet
{
  private:
    std::set<T> FSet;
  public:
    DelphiSet()
    {
    }

    DelphiSet ( T StartValue, T EndValue )
    {
      int Value = EndValue - StartValue;
      if ( StartValue > EndValue )
        throw 1;//ERangeError::CreateFmt ( wxT("Start Value %d is greater than End Value %d"), StartValue, EndValue );
      this->AddRange ( StartValue, Value );
    }

    DelphiSet ( T StartValue, T EndValue , const int Count)
    {
      if ( StartValue > EndValue )
        throw 1;//ERangeError::CreateFmt ( wxT("Start Value %d is greater than End Value %d"), StartValue, EndValue );
      this->AddRange(StartValue,Count);
    }

    DelphiSet ( const DelphiSet<T>& src )
    {
      FSet = src.FSet;
    }

    DelphiSet<T>& operator = ( const DelphiSet<T>& rhs )
    {
      if ( this != &rhs )
      {
        FSet.clear();
        std::set<T>::const_iterator itr;
        for (itr = rhs.FSet.begin(); itr != rhs.FSet.end(); ++itr)
        {
          FSet.insert(*itr);
        }
      }
      return *this;
    }

    DelphiSet<T>& operator += ( const DelphiSet<T>& rhs )
    {
        for (std::set<T>::const_iterator itr = rhs.FSet.begin(); itr != rhs.FSet.end(); itr++)
        {
          FSet.insert(*itr);
        }
      return *this;
    }

    DelphiSet<T>& operator -= ( const DelphiSet<T>& rhs )
    {
        for (std::set<T>::const_iterator itr = rhs.FSet.begin(); itr != rhs.FSet.end(); itr++)
        {
          FSet.erase(*itr);
        }
      return *this;
    }

    DelphiSet<T>& operator *= ( const DelphiSet<T>& rhs )
    {
      for (std::set<T>::const_iterator itr = rhs.FSet.begin(); itr != rhs.FSet.end(); itr++)
      {
        if ( FSet.find ( *itr ) ==  FSet.end() )
          continue;
        FSet.erase ( *itr );
      }
      return *this;
    }

    DelphiSet<T> operator + ( const DelphiSet<T>& rhs ) const
    {
      DelphiSet<T> S = *this;
      for (std::set<T>::const_iterator itr = rhs.FSet.begin(); itr != rhs.FSet.end(); itr++)
      {
        S.FSet.insert(*itr);
      }
      return S;
    }

    DelphiSet<T>& Add ( const T Value )
    {
      FSet.insert ( Value );
      return *this;
    }

    DelphiSet<T>& AddRange ( const T RangeStartValue, const int Count )
    {
      T RangeStartForAdd = RangeStartValue;
      for ( int i = 0 ; i < Count; ++i )
        this->Add ( RangeStartForAdd++ );
      return *this;
    }

    DelphiSet<T>& Add ( const T RangeStartValue, const T RangeEndValue )
    {
      if ( RangeEndValue < RangeStartValue )
        throw 1;//ERangeError::CreateFmt ( wxT("Start Value %d is greater than End Value %d"), RangeStartValue, RangeEndValue );
      int Range = RangeEndValue - RangeStartValue;
      T RangeStartForAdd = RangeStartValue;
      for ( int i = 0 ; i < Range; ++i )
        this->Add ( RangeStartForAdd++ );
      return *this;
    }

    DelphiSet<T>& Remove ( T Value )
    {
      FSet.erase ( Value );
      return *this;
    }

    DelphiSet<T>& Remove ( T RangeStartValue, T RangeEndValue )
    {
      if ( RangeEndValue < RangeStartValue )
        throw 1;//ERangeError::CreateFmt ( wxT("Start Value %d is greater than End Value %d"), RangeStartValue, RangeEndValue );
      for ( T i = RangeStartValue ; i <= RangeEndValue; ++i )
        this->Remove ( i );
      return *this;
    }

    bool Contains ( const T Value ) const
    {
      if ( FSet.find ( Value ) == FSet.end() )
        return false;
      else
        return true;
    }

    bool In ( const T Value ) const
    {
      return Contains ( Value );
    }

    bool Has ( const T Value ) const
    {
      return Contains ( Value );
    }

    void Clear()
    {
      FSet.clear();
    }

    void Empty() const
    {
      FSet.empty();
    }

    bool operator == ( const DelphiSet<T>& rhs ) const
    {
      if ( FSet.size() != rhs.FSet.size() )
        return false;
      for (std::set<T>::const_iterator itr = FSet.begin(); itr != FSet.end(); itr++)
      {
        if ( rhs.FSet.find ( *itr ) == rhs.FSet.end() )
          return false;
      }
      return true;
    }
    bool operator != ( const DelphiSet<T>& rhs ) const
    {
      return !operator== ( rhs );
    }
    static DelphiSet<T>& Init ( T FirstItem, ... )
    {
      DelphiSet<T> *NewOne = new DelphiSet<T>();
      va_list argList;
      NewOne->Add ( FirstItem );
      va_start ( argList, FirstItem );
      T NewItem;
      while ( ( NewItem = va_arg ( argList, T ) ) != 0 )
      {
        NewOne->Add ( NewItem );
      }
      va_end ( argList );
      return *NewOne;
    }

    static DelphiSet<T>& InitRange ( T FirstItem, T LastItem )
    {
      DelphiSet<T> *NewOne = new DelphiSet<T>();
      NewOne->Add ( FirstItem, LastItem );
      return *NewOne;
    }

    static DelphiSet<T>& InitRange ( T FirstItem, T LastItem , const int Count )
    {
      DelphiSet<T> *NewOne = new DelphiSet<T>();
      NewOne->AddRange ( FirstItem, Count);
      return *NewOne;
    }

    bool IsEmpty ( void )
    {
      return ( FSet.size() == 0 );
    }

    wxString ToString ( void )
    {
      wxString Result;
      Result.Alloc ( FSet.size() );
      for (std::set<T>::const_iterator itr = FSet.begin(); itr != FSet.end(); itr++)
      {
        Result += ( wxChar ) *itr;
      }

      return Result;
    }

};

#endif

wxWidgets Form Designer for Delphi and C++ Builder - http://www.twinforms.com

Code snippets for wxWidgets - http://wxsnippets.com

lvdlinden
Earned some good credits
Earned some good credits
Posts: 147
Joined: Thu May 17, 2007 7:03 am
Location: 's-Hertogenbosch, Netherlands

Post by lvdlinden » Tue Jan 01, 2008 8:11 pm

You need to tell the compiler that std::set<T>::const_iterator is a type and not a static member variable. Try:

Code: Select all

typename std::set<T>::const_iterator itr;

gururamnath
Moderator
Moderator
Posts: 466
Joined: Sat Sep 18, 2004 2:49 am
Location: California, USA

Post by gururamnath » Thu Jan 03, 2008 8:55 pm

Hi,
That works like a charm. Thanks for your timely help.

-regards,
Guru Kathiresan
wxWidgets Form Designer for Delphi and C++ Builder - http://www.twinforms.com

Code snippets for wxWidgets - http://wxsnippets.com

Post Reply