Template problem with unordered_map

Discussion in 'C++' started by Paulo Matos, Aug 3, 2006.

  1. Paulo Matos

    Paulo Matos Guest

    HI all,

    I'm using unordered_map from TR1.

    I have a table variable which is a private member of a template class:

    template<typename K, typename D, typename EQ>
    class FooClass {
    public:

    void method(K& key) {
    const unordered_map<K, D, hash<K>, EQ>::const_iterator it =
    table.find(key);
    ....
    }

    private:
    unordered_map<K, D, hash<K>, EQ> table;
    };

    I get an error in the const_iterator line:
    error: expected initializer before 'it'

    Any ideas on what the problem might be?
    I think I have all the includes and using keywords ok (on top of file):
    #include <tr1/unordered_map>

    using std::tr1::unordered_map;
    using std::tr1::hash;


    Any ideas?

    Cheers,

    Paulo Matos
     
    Paulo Matos, Aug 3, 2006
    #1
    1. Advertising

  2. In article <>,
    "Paulo Matos" <> wrote:

    > HI all,
    >
    > I'm using unordered_map from TR1.
    >
    > I have a table variable which is a private member of a template class:
    >
    > template<typename K, typename D, typename EQ>
    > class FooClass {
    > public:
    >
    > void method(K& key) {
    > const unordered_map<K, D, hash<K>, EQ>::const_iterator it =
    > table.find(key);
    > ...
    > }
    >
    > private:
    > unordered_map<K, D, hash<K>, EQ> table;
    > };
    >
    > I get an error in the const_iterator line:
    > error: expected initializer before 'it'
    >
    > Any ideas on what the problem might be?
    > I think I have all the includes and using keywords ok (on top of file):
    > #include <tr1/unordered_map>
    >
    > using std::tr1::unordered_map;
    > using std::tr1::hash;
    >
    >
    > Any ideas?


    Try:

    const typename unordered_map<K, D, hash<K>, EQ>::const_iterator it =

    -Howard
     
    Howard Hinnant, Aug 3, 2006
    #2
    1. Advertising

  3. Paulo Matos

    Paulo Matos Guest

    Howard Hinnant wrote:
    > Try:
    >
    > const typename unordered_map<K, D, hash<K>, EQ>::const_iterator it =
    >


    Hi,

    Thanks a lot, why does that work?
    Why do I need typename?

    > -Howard
     
    Paulo Matos, Aug 3, 2006
    #3
  4. Paulo Matos wrote:
    > Howard Hinnant wrote:
    > > Try:
    > >
    > > const typename unordered_map<K, D, hash<K>, EQ>::const_iterator it =
    > >

    >
    > Hi,
    >
    > Thanks a lot, why does that work?
    > Why do I need typename?
    >


    The compiler doesn't know a priori what sort of a beast this
    "const_iterator" in "unordered_map<K, D, hash<K>, EQ>" might be - maybe
    it's a static int for example, in which case your expression is
    invalid. The typename indicates that it's, well, a typename, so that
    the expression "const Thingy it = Yadda" now makes sense.
     
    tragomaskhalos, Aug 3, 2006
    #4
  5. Paulo Matos

    Paulo Matos Guest

    tragomaskhalos wrote:
    > The compiler doesn't know a priori what sort of a beast this
    > "const_iterator" in "unordered_map<K, D, hash<K>, EQ>" might be - maybe
    > it's a static int for example, in which case your expression is
    > invalid. The typename indicates that it's, well, a typename, so that
    > the expression "const Thingy it = Yadda" now makes sense.


    Yes, thanks a lot. Also found an in-depth explanation in "Effective
    C++" Item 42! :)
     
    Paulo Matos, Aug 3, 2006
    #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. Rares Vernica

    error with tr1 unordered_map iterator

    Rares Vernica, Feb 24, 2007, in forum: C++
    Replies:
    6
    Views:
    1,827
  2. abir
    Replies:
    6
    Views:
    901
    W Karas
    Jun 26, 2008
  3. abir
    Replies:
    3
    Views:
    1,170
  4. Replies:
    2
    Views:
    6,725
  5. Alex Wanderleit
    Replies:
    1
    Views:
    885
    Marcel Müller
    Jan 5, 2011
Loading...

Share This Page