By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

HashTable again

P: n/a
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> >::operator=(const

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

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, b-******@ti.com
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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;
}
[...]

Jul 22 '05 #2

P: n/a
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
kb******@gascad.at
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.