HashTable again

B

Billy Patton

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,
*)((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 said:
& 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, (e-mail address removed)
 
V

Victor Bazarov

Billy said:
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 said:
if (map_iter == this->end()) return false;
key = map_iter->first;
++map_iter;
return true;
}
[...]
 
K

Karl Heinz Buchegger

Billy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top