Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hash_map with custom class pointers

Posted on 2004-04-12
7
Medium Priority
?
1,223 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:edwardt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 

Author Comment

by:edwardt
ID: 10807671
If I do a
handles2->insert(something->getId(), someHandlePtr); i get the following error:

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/hash_map:171: candidates are:
   std::pair<__gnu_cxx::hashtable<std::pair<const _Key, _Tp>, _Key, _HashFcn,
   std::_Select1st<std::pair<const _Key, _Tp> >, _EqualKey, _Alloc>::iterator,
   bool> __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey,
   _Alloc>::insert(__gnu_cxx::hashtable<std::pair<const _Key, _Tp>, _Key,
   _HashFcn, std::_Select1st<std::pair<const _Key, _Tp> >, _EqualKey,
   _Alloc>::value_type&) [with _Key = Id*, _Tp =
   ItemHandle*, _HashFcn = __gnu_cxx::hash<Id*>,
   _EqualKey = std::equal_to<Id*>, _Alloc =
   std::allocator<ItemHandle*>]

0
 
LVL 4

Accepted Solution

by:
bkfirebird earned 1200 total points
ID: 10807825
first declare .....
struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

and then use hash_map as ......
hash_map<const char*, int, hash<const char*>, eqstr> *handles2;
0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 300 total points
ID: 10807880
What type does your method getId() return? If it's Id, then this is your problem. You defined the hash_map with Id* and not with Id.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:edwardt
ID: 10817191
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
0
 

Author Comment

by:edwardt
ID: 10817301
I still get the same errors when I try
handles2[getId()] = stuff;

or

handles2->insert(getId(), stuff);
0
 

Author Comment

by:edwardt
ID: 10818782
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_str());
myfunc(receiver2, receiver2->toString().c_str());

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
0
 

Author Comment

by:edwardt
ID: 10819141
Hi,

The basic problem I'm having is that I need to store hashes as
hashtable[someitem->toString().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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question