Take the 2-minute tour ×

I have an object called hMap declared in a source file in a C++ project as :

dense_hash_map<unsigned char *, int, hash<unsigned char *> > hMap;

where key of the hashmap is of type 'unsigned char array' and value is of type 'int'. I need to pass this object to a function hMap_receive() and should be able to hold the ownership of the object,i.e., hMap_receive() should be able to modify the contents of object hMap.

Question: Should I pass it as a pointer? I passed it checked, but am unable to call two operator overloading methods - array subscription operator & assignment operator(given below), which are public members of class "dense_hash_map".

data_type& operator[](const key_type& key) {       // This is our value-add!
    // If key is in the hashtable, returns find(key)->second,
    // otherwise returns insert(value_type(key, T()).first->second.
    // Note it does not create an empty T unless the find fails.
    return rep.template find_or_insert<DefaultValue>(key).second;

  dense_hashtable& operator= (const dense_hashtable& ht) {
    if (&ht == this)  return *this;        // don't copy onto ourselves
    if (!ht.settings.use_empty()) {
      dense_hashtable empty_table(ht);  // empty table with ht's thresholds
      return *this;


hMap_receive(dense_hash_map<int, unsigned char *, hash<int> > hMap, 
unsigned char *key,int data){
 hMap[key] = data;
 cout << hMap[key];

works fine and assigns data to the key value and prints the data associated with the key. But,

hMap_receive(dense_hash_map<int, unsigned char *, hash<int> > *hMap, 
unsigned char *key,int data){
     hMap[key] = data;
     cout << hMap[key];

neither assigns data not gives the data at key. Rather gives error as :

error: invalid types ‘google::dense_hash_map<unsigned char*, int, 
std::tr1::hash<unsigned char*>, eqstr>*[unsigned char*]’ for array subscript

Why is it not working properly if I pass the object by pointer? If this not the correct way, how should I pass the object so that I will be able to perform all the operations on the object without errors and also be able to modify the original passed object of the caller function.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The [] has a specific meaning on pointer types. If x has type T *, then x[a] means *(x+a), and the result is of type T. So, even if type T has an overloaded [] operator, it doesn't come into play.

So, the error message is about the fact that your dense_hash_map<> does not have the << operator defined for it.

You want to pass a reference to your dense_hash_map<>, not its address.

hMap_receive(dense_hash_map<unsigned char *, int, hash<unsigned char *> > &hMap, 
             unsigned char *key,int data){

Note the replacement if the * with &.

This allows you to call the function passing in your data structure by reference. This means the function is manipulating the object that was used to call the function, not a copy.

dense_hash_map<unsigned char *, int, hash<unsigned char *> > my_map;
hMap_receive(my_map, "foo", 10);
//...my_map may be updated by the function
share|improve this answer
so is this a special case only when operator overloading takes place inside the class?? and to my knowledge reference objects do not take NULL as parameters, so in that case how can we overcome this problem?i.e., can be able to pass even NULL as parameters to the referenced objects –  annunarcist Oct 5 '13 at 23:33
You can either not call the function if the parameter is NULL, or you can write a wrapper function that takes a pointer, and does the null test for you, and calls the reference version if the pointer is valid. –  jxh Oct 6 '13 at 0:18

Your Answer


