Hash map question.

Discussion in 'C++' started by Jim Lynch, Jun 15, 2004.

  1. Jim Lynch

    Jim Lynch Guest

    I've searched google for help with this one, but I haven't found it yet.
    What I want is a hash map this looks like:
    __gnu_cxx::hash_map<string, ArcNode> ArcNodeMap;

    ArcNode is a class. It looks something like this:

    class ArcNode
    {
    private:
    // communication objects
    int fdSocket;
    public:
    ArcNode();
    ~ArcNode();
    int write(string);
    int read(string&);
    int open(string);
    int state();

    };

    But when I try to compile I get:
    In file included from main.cpp:3:
    /usr/include/c++/3.2.2/bits/stl_pair.h: In instantiation of
    `std::pair<const std::string, ArcNode&>':
    /usr/include/c++/3.2.2/ext/stl_hashtable.h:266: instantiated from
    `std::_Select1st<std::pair<const std::string, ArcNode&> >'
    /usr/include/c++/3.2.2/ext/stl_hashtable.h:266: instantiated from
    `__gnu_cxx::hashtable<std::pair<const std::string, ArcNode&>,
    std::string, __gnu_cxx::hash<std::string>,
    std::_Select1st<std::pair<const std::string, ArcNode&> >,
    std::equal_to<std::string>, std::allocator<ArcNode&> >'
    /usr/include/c++/3.2.2/ext/hash_map:101: instantiated from
    `__gnu_cxx::hash_map<std::string, ArcNode&,
    __gnu_cxx::hash<std::string>, std::equal_to<std::string>,
    std::allocator<ArcNode&> >'
    loggerbase.h:48: instantiated from here
    /usr/include/c++/3.2.2/bits/stl_pair.h:84: forming reference to
    reference type
    `ArcNode&'
    /usr/include/c++/3.2.2/ext/hash_map: In instantiation of
    `__gnu_cxx::hash_map<std::string, ArcNode&,
    __gnu_cxx::hash<std::string>, std::equal_to<std::string>,
    std::allocator<ArcNode&> >':
    loggerbase.h:48: instantiated from here
    /usr/include/c++/3.2.2/ext/hash_map:182: forming reference to reference
    type `
    ArcNode&'
    make: *** [logger] Error 1

    I don't understand the reference to reference error message. Can
    someone tell me what I'm doing wrong. A pointer to a tutorial that
    discusses how to put objects into a hash_map would also be helpful.

    Thanks,
    Jim.



    --
    Jim Lynch
     
    Jim Lynch, Jun 15, 2004
    #1
    1. Advertising

  2. Jim Lynch

    Pete C. Guest

    Jim Lynch wrote:
    > I've searched google for help with this one, but I haven't found it
    > yet. What I want is a hash map this looks like:
    > __gnu_cxx::hash_map<string, ArcNode> ArcNodeMap;
    >

    <snip>

    "hash_map" is not Standard C++. Please consult a group for your library.

    - Pete
     
    Pete C., Jun 15, 2004
    #2
    1. Advertising

  3. Jim Lynch

    Buster Guest

    Jim Lynch wrote:
    > I've searched google for help with this one, but I haven't found it yet.
    > What I want is a hash map this looks like:
    > __gnu_cxx::hash_map<string, ArcNode> ArcNodeMap;


    What you seem to have instead is

    > `__gnu_cxx::hash_map<std::string, ArcNode&,
    > __gnu_cxx::hash<std::string>, std::equal_to<std::string>,
    > std::allocator<ArcNode&> >'


    (I might be wrong about this; you didn't post your code and I don't know
    the gnu hash_map implementation well enough to be able to say for sure
    that it doesn't go around adding ampersands willy-nilly like that. I'd
    be surprised though.)

    Don't use a reference type as the second template argument. Reference
    types do not model the concepts required (Assignable for a start).

    Ideally you could read about this at, for example,
    http://www.sgi.com/tech/stl/PairAssociativeContainer.html

    Perhaps best not to take the information from there too seriously in
    this case - the basic situation appears to be:

    (i) Pair Associative Container is a refinement of Container;
    (ii) The value_type of a Container is required to be Assignable;
    (iii) The value_type of a Pair Associative Container is required
    to be std::pair <const key_type, data_type>;
    (iv) std::pair <const key_type, data_type> is not Assignable.

    Therefore 2+2=5, green is pink, and main returns void, QED.

    --
    Regards,
    Buster.
     
    Buster, Jun 15, 2004
    #3
  4. Jim Lynch

    Jorge Rivera Guest

    Jim Lynch wrote:
    > I've searched google for help with this one, but I haven't found it yet.
    > What I want is a hash map this looks like:
    > __gnu_cxx::hash_map<string, ArcNode> ArcNodeMap;
    >


    Try std::hash_map<std::string, ArcNode> ArcNodeMap;

    Although hash_map is not part of the STL (as approved by the standard),
    I know some implementations include it in this namespace. Give it a try.

    > ArcNode is a class. It looks something like this:
    >
    > class ArcNode
    > {
    > private:
    > // communication objects
    > int fdSocket;
    > public:
    > ArcNode();
    > ~ArcNode();
    > int write(string);
    > int read(string&);
    > int open(string);
    > int state();
    >
    > };
    >
    > But when I try to compile I get:
    > In file included from main.cpp:3:
    > /usr/include/c++/3.2.2/bits/stl_pair.h: In instantiation of
    > `std::pair<const std::string, ArcNode&>':
    > /usr/include/c++/3.2.2/ext/stl_hashtable.h:266: instantiated from
    > `std::_Select1st<std::pair<const std::string, ArcNode&> >'
    > /usr/include/c++/3.2.2/ext/stl_hashtable.h:266: instantiated from
    > `__gnu_cxx::hashtable<std::pair<const std::string, ArcNode&>,
    > std::string, __gnu_cxx::hash<std::string>,
    > std::_Select1st<std::pair<const std::string, ArcNode&> >,
    > std::equal_to<std::string>, std::allocator<ArcNode&> >'
    > /usr/include/c++/3.2.2/ext/hash_map:101: instantiated from
    > `__gnu_cxx::hash_map<std::string, ArcNode&,
    > __gnu_cxx::hash<std::string>, std::equal_to<std::string>,
    > std::allocator<ArcNode&> >'
    > loggerbase.h:48: instantiated from here
    > /usr/include/c++/3.2.2/bits/stl_pair.h:84: forming reference to
    > reference type
    > `ArcNode&'
    > /usr/include/c++/3.2.2/ext/hash_map: In instantiation of
    > `__gnu_cxx::hash_map<std::string, ArcNode&,
    > __gnu_cxx::hash<std::string>, std::equal_to<std::string>,
    > std::allocator<ArcNode&> >':
    > loggerbase.h:48: instantiated from here
    > /usr/include/c++/3.2.2/ext/hash_map:182: forming reference to reference
    > type `
    > ArcNode&'
    > make: *** [logger] Error 1
    >


    You need to post code here. This means that whatever you are trying to
    do, it is trying to acquire a reference to a reference, which is illegal
    in C++.

    Jorge L.
     
    Jorge Rivera, Jun 15, 2004
    #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. Amit Bhatia

    Hash Map slower than Map.

    Amit Bhatia, Oct 8, 2007, in forum: C++
    Replies:
    7
    Views:
    381
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Oct 9, 2007
  2. kl
    Replies:
    7
    Views:
    1,324
    James Kanze
    Jan 1, 2008
  3. navS
    Replies:
    3
    Views:
    537
    Ismo Salonen
    May 9, 2008
  4. rp
    Replies:
    1
    Views:
    563
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    658
    David A. Black
    Jul 2, 2008
Loading...

Share This Page