edwardt
asked on
Hash_map with custom class pointers
Hi,
I have two classes
Id and ItemHandle
I am trying to use hash_map to instantiate them and run inserts and find. The code I looked up
class foo{
public:
hash_map<Id*, ItemHandle*> *handles2;
}
foo(){
handles2 = new hash_map<Id*, ItemHandle*>;
}
foo::somecommand(Id* something, ItemHandle* someHandlePtr){
//insert
handles2[something->getId( )] = someHandleptr;
//find
ItemHandle *temp = handles2->find(something-> getId();
//delete
}
but when I compile, i get the following error (for the insert)
file.cpp:70: invalid
types `__gnu_cxx::hash_map<Id*, ItemHandle*,
__gnu_cxx::hash<Id*>, std::equal_to<Id*>,
std::allocator<ItemHandle* > >*[Id*]' for array
subscript
Thanks.
-Edward
I have two classes
Id and ItemHandle
I am trying to use hash_map to instantiate them and run inserts and find. The code I looked up
class foo{
public:
hash_map<Id*, ItemHandle*> *handles2;
}
foo(){
handles2 = new hash_map<Id*, ItemHandle*>;
}
foo::somecommand(Id* something, ItemHandle* someHandlePtr){
//insert
handles2[something->getId(
//find
ItemHandle *temp = handles2->find(something->
//delete
}
but when I compile, i get the following error (for the insert)
file.cpp:70: invalid
types `__gnu_cxx::hash_map<Id*, ItemHandle*,
__gnu_cxx::hash<Id*>, std::equal_to<Id*>,
std::allocator<ItemHandle*
subscript
Thanks.
-Edward
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
bkfirebird
so you're saying that i need to initialize my hash_map with types const char* and int, instead of my custom classes?
khkremer
getId() returns Id*
-Edward
so you're saying that i need to initialize my hash_map with types const char* and int, instead of my custom classes?
khkremer
getId() returns Id*
-Edward
ASKER
I still get the same errors when I try
handles2[getId()] = stuff;
or
handles2->insert(getId(), stuff);
handles2[getId()] = stuff;
or
handles2->insert(getId(), stuff);
ASKER
I made the eqstr stuff and now I have
hash_map<const char*, Receiver*, hash<const char*>, eqstr> handles2;
where my hash function is based on a const char *
I have a function that I use to add things into my hash table, which is called roughly like
myfunc(receiver1, receiver1->toString().c_st r());
myfunc(receiver2, receiver2->toString().c_st r());
void Foo::myfunc(Receiver *receiver, const char *str)
{
hash_map<const char*, Receiver*, hash<const char*>, eqstr>::iterator i;
/*
for (i=handles2.begin(); i!=handles2.end(); i++)
{
const char* key = i->first;
cout << key << ": is what i have" <<endl;
}
*/
i = handles2.find(str);
if (i != handles2.end())
{
cout<<"ERROR - already-registered address "<<str<<endl;
assert(false);
}
addressBook[str] = receiver;
}
The problem is that when I pass in a new string, then my hash table's key entry automatically changes!!!!! :( ARGH!!!! That means if my first pair is
"hello", Recever1
and my second pair that i pass in is
"hello2", Receiver2
then it changes my original hash value from "hello" to "hello2"......
Thanks.
-Edward
hash_map<const char*, Receiver*, hash<const char*>, eqstr> handles2;
where my hash function is based on a const char *
I have a function that I use to add things into my hash table, which is called roughly like
myfunc(receiver1, receiver1->toString().c_st
myfunc(receiver2, receiver2->toString().c_st
void Foo::myfunc(Receiver *receiver, const char *str)
{
hash_map<const char*, Receiver*, hash<const char*>, eqstr>::iterator i;
/*
for (i=handles2.begin(); i!=handles2.end(); i++)
{
const char* key = i->first;
cout << key << ": is what i have" <<endl;
}
*/
i = handles2.find(str);
if (i != handles2.end())
{
cout<<"ERROR - already-registered address "<<str<<endl;
assert(false);
}
addressBook[str] = receiver;
}
The problem is that when I pass in a new string, then my hash table's key entry automatically changes!!!!! :( ARGH!!!! That means if my first pair is
"hello", Recever1
and my second pair that i pass in is
"hello2", Receiver2
then it changes my original hash value from "hello" to "hello2"......
Thanks.
-Edward
ASKER
Hi,
The basic problem I'm having is that I need to store hashes as
hashtable[someitem->toStri ng().c_str ()] = value;
where the result of .c_str() is lets say, "mykey"
but i need to access them elsewhere using something like
hashtable.find("mykey");
Right now, it doesn't seem to work, because my hash table (i think) stores the address of c_str() instead of a literal (which I want). Anyway to convert string to a string literal :P?
Thanks.
-Edward
The basic problem I'm having is that I need to store hashes as
hashtable[someitem->toStri
where the result of .c_str() is lets say, "mykey"
but i need to access them elsewhere using something like
hashtable.find("mykey");
Right now, it doesn't seem to work, because my hash table (i think) stores the address of c_str() instead of a literal (which I want). Anyway to convert string to a string literal :P?
Thanks.
-Edward
ASKER
handles2->insert(something
file.cpp:71: no
matching function for call to `__gnu_cxx::hash_map<Id*,
ItemHandle*, __gnu_cxx::hash<Id*>,
std::equal_to<Id*>, std::allocator<ItemHandle*
insert(Id*, ItemHandle*&)'
/usr/include/c++/3.2/ext/h
std::pair<__gnu_cxx::hasht
std::_Select1st<std::pair<
bool> __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
_Alloc>::insert(__gnu_cxx:
_HashFcn, std::_Select1st<std::pair<
_Alloc>::value_type&) [with _Key = Id*, _Tp =
ItemHandle*, _HashFcn = __gnu_cxx::hash<Id*>,
_EqualKey = std::equal_to<Id*>, _Alloc =
std::allocator<ItemHandle*