HashTable again

Discussion in 'C++' started by Billy Patton, Dec 9, 2004.

  1. Billy Patton

    Billy Patton Guest

    I've had a lot of help, but I need a little more.

    Here's a snippett of code:
    template <typename K,typename V>
    class HashTable : private std::map<K, V>
    {
    private:

    public:
    bool Keys(K& key, bool firstp = false) const
    {
    static typename std::map<K,V>::iterator map_iter;
    if (firstp) map_iter = this->begin(); <<<<<<<<<<<<LINE #81
    if (map_iter == this->end()) return false;
    key = map_iter->first;
    ++map_iter;
    return true;
    }


    I need to call this method until it has hit all the keys. Thst's why I declare
    map_iter as static.
    HashTable<int,std::string> h;
    int i;
    bool first=true;
    while (h.Keys(i,first))
    {
    first = false;
    do_something_with_i();
    }

    Here's my actual test code:
    void t_HashTable(void)
    {
    ENTER
    HashTable<int,std::string> h;
    pass(0 EQ h.EntryCount() , "verify hash is empty");
    pass(true EQ h.Put(1,"a") , "put a into 1");
    pass(1 EQ h.EntryCount() , "verify hash hasn 1 entry");
    pass(h.Value(1) EQ "a" , "Verify value of 1 is a");
    int i;
    pass(h.Keys(i,true) , "Get first key"); <<<<<<< LINE# 142
    pass(i EQ 1 , "Verify only key is 1");

    LEAVE
    }


    Here's my compiler message:
    x.cxx: In member function `bool HashTable<K, V>::Keys(K&, bool) const [with K =
    int, V = std::string]':
    x.cxx:142: instantiated from here
    x.cxx:81: error: no match for 'operator=' in 'map_iter = ((const std::map<int,
    std::string, std::less<int>, std::allocator<std::pair<const int, std::string> >
    >*)((const HashTable<int, std::string>*)this))->std::map<_Key, _Tp, _Compare,

    _Alloc>::begin [with _Key = int, _Tp = std::string, _Compare = std::less<int>,
    _Alloc = std::allocator<std::pair<const int, std::string> >]()'
    /apps/gcc/3.4.1/lib/gcc/i686-pc-linux-gnu/3.4.1/../../../../include/c++/3.4.1/bits/stl_tree.h:152:
    note: candidates are: std::_Rb_tree_iterator<std::pair<const int, std::string>
    >& std::_Rb_tree_iterator<std::pair<const int, std::string> >::eek:perator=(const

    std::_Rb_tree_iterator<std::pair<const int, std::string> >&)





    ___ _ ____ ___ __ __
    / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    /___/
    Texas Instruments ASIC Circuit Design Methodology Group
    Dallas, Texas, 214-480-4455,
    Billy Patton, Dec 9, 2004
    #1
    1. Advertising

  2. Billy Patton wrote:
    > I've had a lot of help, but I need a little more.
    >
    > Here's a snippett of code:
    > template <typename K,typename V>
    > class HashTable : private std::map<K, V>
    > {
    > private:
    >
    > public:
    > bool Keys(K& key, bool firstp = false) const
    > {
    > static typename std::map<K,V>::iterator map_iter;
    > if (firstp) map_iter = this->begin(); <<<<<<<<<<<<LINE #81


    Since 'Keys' is a const function, only a const 'begin' can be called.
    The const 'begin' returns 'const_iterator' value, not 'iterator'.
    Declare your 'map_iter' like this:

    static typename std::map<K,V>::const_iterator map_iter;

    > if (map_iter == this->end()) return false;
    > key = map_iter->first;
    > ++map_iter;
    > return true;
    > }
    > [...]
    Victor Bazarov, Dec 9, 2004
    #2
    1. Advertising

  3. Billy Patton wrote:
    >
    > I've had a lot of help, but I need a little more.
    >

    In addition to Victor's help:

    What you are trying to do is an extremely bad idea.
    It prevents the user of this class to have 2 HashTable
    objects and iterate through them at the same time. Why?
    Simple: Because there is only *1* iteration variable when
    2 are needed.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Dec 9, 2004
    #3
    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
    Replies:
    0
    Views:
    372
  2. che
    Replies:
    2
    Views:
    475
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    316
    Maric Michaud
    Jun 9, 2006
  4. Replies:
    4
    Views:
    391
  5. Ò»Ê×Ê«

    A design problem I met again and again.

    Ò»Ê×Ê«, Apr 1, 2009, in forum: Python
    Replies:
    24
    Views:
    692
    Dennis Lee Bieber
    Apr 4, 2009
Loading...

Share This Page