g++4.3.2 w/ c++0x: error: no type named ‘iterator’ in ‘class std::unordered_map<...'

Discussion in 'C++' started by Samuel.Hornus@gmail.com, Nov 22, 2008.

  1. Guest

    Hi,
    I have a problem with g++ 4.3.2 in -std=c++0x mode.
    Here is some code which works fine:

    struct My_data
    {
    typedef std::map<Key, My_data, Compare> My_Map; (***)
    typedef typename My_map::iterator My_iterator;
    // data members
    My_iterator cousin_;
    };
    typedef std::map<Key, My_data, Compare> My_Map;
    My_map Family;

    Note, that it uses My_data as the data_value for the std::map type
    defined in the My_data struct *itself*.
    This works fine. But now I want to change std::map by
    std::unordered_map.
    So do I, with appropriate hash function and Equality comparator.
    But then, g++ 4.3.2 gives me the following error:

    error: no type named ‘iterator’ in ‘class std::unordered_map<...'
    at line (***). I know this does come from the fact that I'm using
    My_data as a template parameter inside the definition of the struct
    My_data itself.
    But... it did work for std::map... so why shouldn't it work for
    unordered_map ?
    Is there a workaround ? is this a bug of libstd++ ?
    Thank you very much !
    sam
     
    , Nov 22, 2008
    #1
    1. Advertising

  2. Alan Johnson Guest

    wrote:
    > Hi,
    > I have a problem with g++ 4.3.2 in -std=c++0x mode.
    > Here is some code which works fine:
    >
    > struct My_data
    > {
    > typedef std::map<Key, My_data, Compare> My_Map; (***)
    > typedef typename My_map::iterator My_iterator;
    > // data members
    > My_iterator cousin_;
    > };
    > typedef std::map<Key, My_data, Compare> My_Map;
    > My_map Family;
    >
    > Note, that it uses My_data as the data_value for the std::map type
    > defined in the My_data struct *itself*.
    > This works fine. But now I want to change std::map by
    > std::unordered_map.
    > So do I, with appropriate hash function and Equality comparator.
    > But then, g++ 4.3.2 gives me the following error:
    >
    > error: no type named ‘iterator’ in ‘class std::unordered_map<...'
    > at line (***). I know this does come from the fact that I'm using
    > My_data as a template parameter inside the definition of the struct
    > My_data itself.
    > But... it did work for std::map... so why shouldn't it work for
    > unordered_map ?
    > Is there a workaround ? is this a bug of libstd++ ?
    > Thank you very much !
    > sam
    >


    This may have changed in C++0X (I haven't checked), but in the 2003
    standard what you are doing is undefined behavior. You cannot
    instantiate a template component for any of the standard library
    templates with an incomplete type. Within the definition of My_data,
    My_data is an incomplete type.

    Reference 17.4.3.6.2 in the 2003 standard.

    --
    Alan
     
    Alan Johnson, Nov 23, 2008
    #2
    1. Advertising

  3. Bo Persson Guest

    wrote:
    > Hi,
    > I have a problem with g++ 4.3.2 in -std=c++0x mode.
    > Here is some code which works fine:
    >
    > struct My_data
    > {
    > typedef std::map<Key, My_data, Compare> My_Map; (***)
    > typedef typename My_map::iterator My_iterator;
    > // data members
    > My_iterator cousin_;
    > };
    > typedef std::map<Key, My_data, Compare> My_Map;
    > My_map Family;
    >
    > Note, that it uses My_data as the data_value for the std::map type
    > defined in the My_data struct *itself*.
    > This works fine. But now I want to change std::map by
    > std::unordered_map.
    > So do I, with appropriate hash function and Equality comparator.
    > But then, g++ 4.3.2 gives me the following error:
    >
    > error: no type named ‘iterator’ in ‘class std::unordered_map<...'
    > at line (***). I know this does come from the fact that I'm using
    > My_data as a template parameter inside the definition of the struct
    > My_data itself.
    > But... it did work for std::map... so why shouldn't it work for
    > unordered_map ?
    > Is there a workaround ? is this a bug of libstd++ ?
    > Thank you very much !
    > sam


    It just happened to work for std::map, even though it is not required.

    The workaround would possibly be to store a pointer or a reference to
    the cousin, instead of an iterator.


    Bo Persson
     
    Bo Persson, Nov 23, 2008
    #3
  4. Guest

    On 23 nov, 11:00, "Bo Persson" <> wrote:
    > It just happened to work for std::map, even though it is not required.
    >
    > The workaround would possibly be to store a pointer or a reference to
    > the cousin, instead of an iterator.


    And Alan wrote:
    > Reference 17.4.3.6.2 in the 2003 standard.


    Thank you Alan and Bo for your prompt replies.
    I'm very disappointed. It is very important for my algorithm
    to store these iterators into the data_type itself. So I'll try to
    hard code
    the iterator type into my My_data structure, by copying from the
    standard header.
    I'll see if that works.
    Do you know any reference explaining how we can achieve this somewhat
    cleanly ?
    Maybe using another template library. I don't care using something
    other than the STL.
    Thanks again,
    sam
     
    , Nov 23, 2008
    #4
    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. Replies:
    6
    Views:
    662
    Jim Langston
    Oct 30, 2005
  2. Rares Vernica

    error with tr1 unordered_map iterator

    Rares Vernica, Feb 24, 2007, in forum: C++
    Replies:
    6
    Views:
    1,816
  3. abir
    Replies:
    6
    Views:
    878
    W Karas
    Jun 26, 2008
  4. K. Frank
    Replies:
    3
    Views:
    514
    K. Frank
    May 31, 2013
  5. Rahul Mathur
    Replies:
    1
    Views:
    198
    Victor Bazarov
    Feb 20, 2014
Loading...

Share This Page