CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    3

    C++ STL sample code by Rodney Myers does not compile

    The code does not compile as-is.

    Original link:
    http://frank.mtsu.edu/~csjudy/STL/HashMap.h

    Here is the bare-bone code:

    #include <vector>
    #include <map>

    /*
    HashMap class template; A usage example for vector, map, and function objects.
    Written by Rodney Myers, 3/4/00
    */

    namespace Containers
    {
    template <class Key, class Value, class HashFunc, class Pred=std::less<Key> >
    class HashMap: private std::vector<std::map<Key, Value, Pred> >
    {
    public:
    explicit HashMap(int hashFunctionUpperBound)
    {
    hashFunc = HashFunc();
    resize(hashFunctionUpperBound+1);
    }

    Value& operator[](Key& key)
    {
    return at(hashFunc(key))[key];
    }

    bool Find(const Key &key, Value &val) const
    {
    std::map<Key, Value, Pred>::const_iterator itr;
    int index = hashFunc(key);

    // search for the key value
    if ((itr = at(index).find(key)) == at(index).end())
    return false; // key not found

    // the key was found, copy the associated value
    val = itr->second;
    return true;
    }

    HashFunc hashFunc; // used to store an instance of the hash function object
    };

    }

    However, if I replace the "Key" to be string and "Value" to be "int", it compiles fine.
    I don't have a clue as to why this is the case though. Any one has a clue??

  2. #2

    Re: C++ STL sample code by Rodney Myers does not compile

    You probably have those defined already.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: C++ STL sample code by Rodney Myers does not compile

    The code is indeed erroneous. You need to qualify superclass functions and a typename is missing:
    Code:
    #include <vector>
    #include <map>
    
    /*
    HashMap class template; A usage example for vector, map, and function objects.
    Written by Rodney Myers, 3/4/00
    */
    
    namespace Containers
    {
            template <class Key, class Value, class HashFunc, class Pred=std::less<Key> >
            class HashMap: private std::vector<std::map<Key, Value, Pred> >
            {
            public:
                    explicit HashMap(int hashFunctionUpperBound)
                    {
                            hashFunc = HashFunc();
                            std::vector<std::map<Key, Value, Pred> >::resize(hashFunctionUpperBound+1);
                    }
    
                    Value& operator[](Key& key)
                    {
                            return std::vector<std::map<Key, Value, Pred> >::at(hashFunc(key))[key];
                    }
    
                    bool Find(const Key &key, Value &val) const
                    {
                            typename std::map<Key, Value, Pred>::const_iterator itr;
                            int index = hashFunc(key);
    
                            // search for the key value
                            if ((itr = std::vector<std::map<Key, Value, Pred> >::at(index).find(key)) == std::vector<std::map<Key, Value, Pred> >::at(index).end())
                                     return false; // key not found
    
                            // the key was found, copy the associated value
                            val = itr->second;
                            return true;
                    }
    
                    HashFunc hashFunc; // used to store an instance of the hash function object
            };
    
    }
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ STL sample code by Rodney Myers does not compile

    If an update is indeed made, I would suggest that there be a (private) typedef, e.g.,
    Code:
    typedef std::vector<std::map<Key, Value, Pred> > parent;
    so that the implementation can use parent::at(index) instead of the terribly verbose std::vector<std::map<Key, Value, Pred> >::at(index).

    EDIT:
    Oh yeah, but now I remember a simpler way to fix this. One can use this-> instead of a typedef:
    Code:
    #include <vector>
    #include <map>
    
    /*
    HashMap class template; A usage example for vector, map, and function objects.
    Written by Rodney Myers, 3/4/00
    */
    
    namespace Containers
    {
            template <class Key, class Value, class HashFunc, class Pred=std::less<Key> >
            class HashMap: private std::vector<std::map<Key, Value, Pred> >
            {
            public:
                    explicit HashMap(int hashFunctionUpperBound)
                    {
                            hashFunc = HashFunc();
                            this->resize(hashFunctionUpperBound+1);
                    }
    
                    Value& operator[](Key& key)
                    {
                            return this->at(hashFunc(key))[key];
                    }
    
                    bool Find(const Key &key, Value &val) const
                    {
                            typename std::map<Key, Value, Pred>::const_iterator itr;
                            int index = hashFunc(key);
    
                            // search for the key value
                            if ((itr = this->at(index).find(key)) == this->at(index).end())
                                     return false; // key not found
    
                            // the key was found, copy the associated value
                            val = itr->second;
                            return true;
                    }
    
                    HashFunc hashFunc; // used to store an instance of the hash function object
            };
    
    }
    Last edited by laserlight; November 24th, 2009 at 03:37 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured