Assignement operator, templates, double associative array

Discussion in 'C++' started by mscava@gmail.com, Feb 20, 2007.

  1. Guest

    I'm building DataManager<T>. A class where shared data will be stored.
    One of things useful to implement is garbage collection. But it still
    gives me this error:

    stl_algo.h:1076: error: non-static const member `const std::string
    DataMapPair::first', can't use default assignment operator

    Here's my code:

    typedef std::map< std::string, std::pair< bool, CountedPtr<T> > >
    DataMap;
    typedef std::pair< std::string, std::pair< bool, CountedPtr<T> > >
    DataMapPair;

    DataMap dataMap_;

    template <typename T> bool DataManager<T> ::
    IsGarbage( const DataMapPair& dmPair )
    {
    return ( dmPair.second.first && dmPair.second.second.IsUnique() );
    }

    template <typename T> void DataManager<T> ::
    CollectGarbage()
    {
    dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
    IsGarbage ), dataMap_.end() );
    }

    Can anyone tell how should I oveload that operator=? Or should I do
    something else?
     
    , Feb 20, 2007
    #1
    1. Advertising

  2. wrote:
    > I'm building DataManager<T>. A class where shared data will be stored.
    > One of things useful to implement is garbage collection. But it still
    > gives me this error:
    >
    > stl_algo.h:1076: error: non-static const member `const std::string
    > DataMapPair::first', can't use default assignment operator
    >
    > Here's my code:
    >
    > typedef std::map< std::string, std::pair< bool, CountedPtr<T> > >
    > DataMap;
    > typedef std::pair< std::string, std::pair< bool, CountedPtr<T> > >
    > DataMapPair;
    >
    > DataMap dataMap_;
    >
    > template <typename T> bool DataManager<T> ::
    > IsGarbage( const DataMapPair& dmPair )
    > {
    > return ( dmPair.second.first && dmPair.second.second.IsUnique() );
    > }
    >
    > template <typename T> void DataManager<T> ::
    > CollectGarbage()
    > {
    > dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
    > IsGarbage ), dataMap_.end() );
    > }
    >
    > Can anyone tell how should I oveload that operator=? Or should I do
    > something else?
    >


    remove_if tries to rearrange the sequence it is given. std::map's are
    stored in a fixed order (i.e. they are ordered by a key) and they don't
    like it when you try to rearrange them. That is what the error message
    means.

    Instead of trying fancy stuff in your CollectGarbage method you should
    just write a loop through the entries of the map and delete any that are
    garbage.

    john
     
    John Harrison, Feb 20, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 20, 6:56 pm, John Harrison <> wrote:
    > wrote:
    > > I'm building DataManager<T>. A class where shared data will be stored.
    > > One of things useful to implement is garbage collection. But it still
    > > gives me this error:

    >
    > > stl_algo.h:1076: error: non-static const member `const std::string
    > > DataMapPair::first', can't use default assignment operator

    >
    > > Here's my code:

    >
    > > typedef std::map< std::string, std::pair< bool, CountedPtr<T> > >
    > > DataMap;
    > > typedef std::pair< std::string, std::pair< bool, CountedPtr<T> > >
    > > DataMapPair;

    >
    > > DataMap dataMap_;

    >
    > > template <typename T> bool DataManager<T> ::
    > > IsGarbage( const DataMapPair& dmPair )
    > > {
    > > return ( dmPair.second.first && dmPair.second.second.IsUnique() );
    > > }

    >
    > > template <typename T> void DataManager<T> ::
    > > CollectGarbage()
    > > {
    > > dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
    > > IsGarbage ), dataMap_.end() );
    > > }

    >
    > > Can anyone tell how should I oveload that operator=? Or should I do
    > > something else?

    >
    > remove_if tries to rearrange the sequence it is given. std::map's are
    > stored in a fixed order (i.e. they are ordered by a key) and they don't
    > like it when you try to rearrange them. That is what the error message
    > means.
    >
    > Instead of trying fancy stuff in your CollectGarbage method you should
    > just write a loop through the entries of the map and delete any that are
    > garbage.
    >
    > john


    Thank you very much... I should have known.
     
    , Feb 20, 2007
    #3
  4. Andre Kostur Guest

    wrote in news:1171994616.455888.110910
    @a75g2000cwd.googlegroups.com:

    >>
    >> > typedef std::map< std::string, std::pair< bool, CountedPtr<T> > >
    >> > DataMap;
    >> > typedef std::pair< std::string, std::pair< bool, CountedPtr<T> > >
    >> > DataMapPair;



    Out of curiosity, any reason why the second line isn't:

    typedef DataMap::value_type DataMapPair;

    ? Or simply use DataMap::value_type directly?
     
    Andre Kostur, Feb 20, 2007
    #4
  5. Guest

    > Out of curiosity, any reason why the second line isn't:
    >
    > typedef DataMap::value_type DataMapPair;
    >
    > ? Or simply use DataMap::value_type directly?


    I'm still not so IN the STL as I should be... And I didn't know
    construction like this exists. Currently reading a book about STL form
    Josuttis, so hopefully I'll get better.
     
    , Feb 20, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    484
  2. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  3. Alexander Eisenhuth

    Looking for assignement operator

    Alexander Eisenhuth, Oct 17, 2006, in forum: Python
    Replies:
    17
    Views:
    486
    Bruno Desthuilliers
    Oct 25, 2006
  4. desktop
    Replies:
    5
    Views:
    388
    James Kanze
    Jun 26, 2007
  5. Replies:
    5
    Views:
    179
    Stuart
    Jun 30, 2013
Loading...

Share This Page