473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

hash_map in STL.

Hi,
I am trying to use hash maps from STL on gcc 3.3 as follows:

#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
class Node;
typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;

class Node
{ //everything else;
Loc_Tree::iterator parent;
};
#endif

And when compiling this does not compile, compiler complains about
forward declaration of Node and says that incomplete type being used in
Loc_Tree.

To correct this, I changed as follows:

#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
using namespace __gnu_cxx;
class Node
{ //everything else;
hash_map<pair<int,int>, Node, Node_Hasher>::iterator parent;
};

typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
#endif

Now the compiler as expected still complains: error: `std::pair<_T1,
_T2>::second' has incomplete type

I googled for this and it was suggested somewhere to define a base class
(say Base_Node) and then make Node derived class and use Base_Node in
hash_map as follows:

base_bode.h looks like:

#ifndef BASE_NODE_H
#define BASE_NODE_H
class Base_Node{;};
#endif
and node.h looks like:

#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
#include "base_node.h"
using namespace __gnu_cxx;

typedef hash_map<pair<int,int>, Base_Node, Node_HasherLoc_Tree;

class Node:public Base_Node
{ //everything else;
Loc_Tree::iterator parent;
};

#endif

And now it compiles.
However, when I try to insert an object of type Node in Loc_Tree (in
some other .C file), the compiler complains that it wants Base_Node and
I am giving it Node.

How can I fix this problem? If I want to use Node in hash_map, then I
can't use hash_map inside the class declaration.
thanks,
--a.
Sep 19 '07 #1
2 4252
Amit Bhatia wrote:
Hi,
I am trying to use hash maps from STL on gcc 3.3 as follows:

#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
class Node;
typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;

class Node
{ //everything else;
Loc_Tree::iterator parent;
};
#endif

And when compiling this does not compile, compiler complains about
forward declaration of Node and says that incomplete type being used in
Loc_Tree.

To correct this, I changed as follows:

#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
using namespace __gnu_cxx;
class Node
{ //everything else;
hash_map<pair<int,int>, Node, Node_Hasher>::iterator parent;
};

typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
I don't know gnu c++ lib,
But I guess most hash_map implementation should go like this

hash_map<Key, Value, ....>

for compatible usage for map, multimap
#endif

Now the compiler as expected still complains: error: `std::pair<_T1,
_T2>::second' has incomplete type

I googled for this and it was suggested somewhere to define a base class
(say Base_Node) and then make Node derived class and use Base_Node in
hash_map as follows:

base_bode.h looks like:

#ifndef BASE_NODE_H
#define BASE_NODE_H
class Base_Node{;};
#endif
and node.h looks like:

#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
#include "base_node.h"
using namespace __gnu_cxx;

typedef hash_map<pair<int,int>, Base_Node, Node_HasherLoc_Tree;

class Node:public Base_Node
{ //everything else;
Loc_Tree::iterator parent;
};

#endif

And now it compiles.
However, when I try to insert an object of type Node in Loc_Tree (in
some other .C file), the compiler complains that it wants Base_Node and
I am giving it Node.

How can I fix this problem? If I want to use Node in hash_map, then I
can't use hash_map inside the class declaration.
thanks,
--a.

--
Thanks
Barry
Sep 19 '07 #2
On Sep 19, 2:38 pm, Amit Bhatia <abha...@nospam.nospam.comwrote:
I am trying to use hash maps from STL on gcc 3.3 as follows:
[Various attempts to create a member of Node which is a
hash_map of Nodes deleted...]
How can I fix this problem? If I want to use Node in hash_map, then I
can't use hash_map inside the class declaration.
Because hash_map, like the standard containers, requires a
complete type to be instantiated. And a class type is only
complete when the final } of the class definition has been seen.
The only solution is to introduce ponters or references
somewhere, e.g.:

class Node
{
// ...
hash_map< pair< int, int >, Node*, Node_Hasher >
parent ;
} ;

or

class Node
{
// ...
hash_map< pair< int, int >, Node, Node_Hasher >*
parent ;
} ;

(I suspect that the first is probably what you want anyway.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Florian Liefers | last post by:
"Hello World\n", i get error C2143 (Syntaxerror, missing ';' before '<') using the following code: #include <hash_map> struct eqstr { bool operator()(const char* s1, const char* s2) const
10
by: Jon Cosby | last post by:
I need help in hashmaps. Why doesn't this work: #include <hash_map> hash_map <int, string> hm1; typedef pair <int, string> pr; hm1.insert(str_pair(1, "Hello")); It compiles, but crashes at...
3
by: Mark | last post by:
Hi, I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It will compile if I use <map> but I get a bunch of template compile errors when I change it to hash_map. Any...
5
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
3
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently...
1
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, it is not available in Apache since it is not...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
5
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: float a; float b; ...
2
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.