By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

hash_map in STL.

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.