views:

110

answers:

4

I had a

HashMap<Node, Double> 

in Java which I'd use later on to retrieve the double associated with a node. I've tried to do

boost::unordered_map<Node*, double> 

but I get a "error C2108: subscript is not of integral type" when I try to put something in it, like:

map[some_node] = some_double;

If I interpreted the error right, then I have to replace my double with an int. Is there a simple way around this?

okay, here's the function then:

void myClass::someFunction(const double* r)
{
    //map is boost::unordered_map<Node*, double> 
    //nodes is a pointer to std::vector<Node*>
    std::vector<Node*>::iterator it;
    for(it = nodes->begin(); it != nodes->end(); it++)
    {
        //calculate the index
        map[*it] = r[index]; //error
    }
}
A: 

It's not complaining about the double, it's complaining about "some_node".

What is your map defined as specifically?

Joe
A: 

You don't give the declaration of some_node but you would get this error if some_node is not a pointer. The double should be fine.

So you might need something like this:

Node some_node;
...
map[&some_node] = some_double; 
x4u
+1  A: 

The error is not for the map access, but for r[index]. index must be an integer type.

interjay
oh yeah, can't believe i didn't notice that. i declared index as double. thanks!
zxcvbnm
+1  A: 

Unlike Java, C++ does not provide hashing functions for classes. If the type of the hashmap key is an integer or a pointer, then C++ can use the fact that an integer is its own hash, but it can't fo this for types you define yourself - in that case you have to provide a hash function explicitly. This can be hard to do efficiently, which is one reason that hashes were excluded from the original C++ standard in favour of maps that use a tree structure rather than a hash table, and only require operator<() to be defined, which is usually much easier to write than an efficient hash function.

I'd also observe that if you are using a pointer to a node as the hash key, it may be easier and quicker to store the double value in the node itself, rather than use a hashtable, as you effectively already have the node you want to hand.

anon
while this isn't what i was looking for, you're right. it's better to just add the double to the Node.
zxcvbnm