0
4

2 回答 2

2

vertices and edges are local variables in your constructor, shadowing the instance variables of the same name. To initialize the instance variables to empty hash tables, just remove those variables from your constructor:

Markovgraph::Markovgraph() {
    // note: no declarations of vertices and edges here
    vertices.set_empty_key(NULL);
    edges.set_empty_key(NULL);

    vertices["a"] = 10000;
    vertices["b"] = 573;
    std::cout << vertices["a"] << std::endl;
    std::cout << vertices["b"] << std::endl;
    std::cout << vertices.size() << std::endl;
}

This works because the constructor implicitly calls the default constructor for the instance variables.

于 2013-10-10T14:05:37.330 回答
1

Here, in your constructor:

google::dense_hash_map<const char*, long, MurmurHasher<const char*>, eqstr> vertices;

You have defined a new variable called vertices, which has the scope and lifetime of the constructor. You need to just remove this line, because an identical variable already exists as a member of the class. What you have done is called "shadowing", and your compiler should warn you about it if you use suitable options like -Wall.

于 2013-10-10T14:09:14.593 回答