How to write this recursive definition?

L

lkrich7

hello, I wrote the following definition:

typedef vector< pair<char, TrieNode*> > TrieNode;

but it can't be compiled, because "TrieNode" is not declared
in fact my aim is:
struct Pair;
typedef vector<Pair> TrieNode;
struct Pair{
char key;
TrieNode *ptr;
Pair(char k, TrieNode *p){ key = k; ptr = p;}
};
and I want to use template "pair" from STL.

How to write this defintion?

Thanks very much!
 
T

Thomas Tutone

Sinc it is a pointer add (i.e forward declare)

class TrieNode;

Before using TrieNode

Uhhm, did you try this before posting? Because I can't imagine that
would compile. TrieNode isn't a class, it's a typedef of a template
instantiation.

Best regards,

Tom

 
R

Radu

hello, I wrote the following definition:

typedef vector< pair<char, TrieNode*> > TrieNode;

but it can't be compiled, because "TrieNode" is not declared
in fact my aim is:
struct Pair;
typedef vector<Pair> TrieNode;
struct Pair{
char key;
TrieNode *ptr;
Pair(char k, TrieNode *p){ key = k; ptr = p;}
};
and I want to use template "pair" from STL.

How to write this defintion?
most probably all you can do is:

struct Pair;
typedef std::vector<Pair> TrieNode;
struct Pair{
typedef std::pair<char, TrieNode *> PAIR;
PAIR p_;
Pair(const PAIR& p)
{
p_ = p;
}
operator PAIR()
{
return p_;
}
};

to be used like std::pair:

void f()
{
TrieNode t;
t.push_back(std::make_pair('c', &t));
Pair::pAIR p = t[0];
}

using std::pair directly creates a cyclic dependency that cannot be
broken using forward declarations - the second template parameters'
type has to be known in advance.


HTH
Radu
 
M

Michael

How about this:

struct TrieNode : public vector< pair<char, struct TrieNode*> >
{
};
 
D

David Harmon

On Mon, 11 Sep 2006 09:31:20 -0400 in comp.lang.c++, "Victor
Bazarov said:

Since std::vector has no virtual functions, inheriting from it is
usually based on some mistaken idea about what you might accomplish
thereby. Not always.
 
V

Victor Bazarov

David said:
On Mon, 11 Sep 2006 09:31:20 -0400 in comp.lang.c++, "Victor


Since std::vector has no virtual functions, inheriting from it is
usually based on some mistaken idea about what you might accomplish
thereby.

Huh? Since when does inheriting require virtual functions? Why does
it always necessary to assume the worst when giving out language advice?
Not always.

I don't see much sense in this explanation, sorry. It seems to me that
many a recommendation of this sort have the same basis as McDonald's
warning about the temperature of the contents of their coffee cups.
We are not bound by the same laws, we should stop making excuses for
the beginners not to learn the damn language. "Oh, don't inherit from
this class, sweetheart, it has no virtual functions, you might not
get what you think you might get..." Bullsh*t. Let them try it. If
it's too hot, they will learn much better than if you keep telling them
not to drink it (usually).

V
 
N

Noah Roberts

Victor said:
Huh? Since when does inheriting require virtual functions? Why does
it always necessary to assume the worst when giving out language advice?


I don't see much sense in this explanation, sorry. It seems to me that
many a recommendation of this sort have the same basis as McDonald's
warning about the temperature of the contents of their coffee cups.

Well you know why they did that don't you? They got sued for having
their coffee at such a high temperature that it caused 3rd degree burns
when spilled. The judge found that keeping it at such a high
temperature was negligent and found for the plaintifs. That warning is
McDonald's way of saying, "**** you, idiots." It's debatable...it's
pretty dumb to expect that hot coffee isn't hot but then you don't
expect it to remove flesh either. It does seem rather negligent and
underhanded to hand someone coffee that will burn the flesh off their
bones to someone about to drive off in a car.
We are not bound by the same laws, we should stop making excuses for
the beginners not to learn the damn language. "Oh, don't inherit from
this class, sweetheart, it has no virtual functions, you might not
get what you think you might get..." Bullsh*t. Let them try it. If
it's too hot, they will learn much better than if you keep telling them
not to drink it (usually).

Well, it is good to warn them because the errors that can come about
are rather...interesting, to say the least. I think it sufficient
though to just say that the subclass will not be a polymorphic vector
and leave it at that...in other words, never, ever, pass it to a
function that wants to work on a vector that will call any of the
overridden functions.

Usually though, when people subclass vector it is so they can add
behavior without having to add wrappers for all the functions vector
has, as would be necissary with composition (the more theoretically
sound choice). So usually you're not overriding behavior anyway; when
you do you just have to keep in mind that it isn't polymorphic in any
fassion. Add to that the fact that much of the time vectors are passed
to templated functions so the issue is moot in that regard. In the end
there are a few times when you need to worry about it: when you want to
pass to a function expecting a vector of the type you are containing;
or when you want to pass to a template explicitly expecting a vector
(and then it just won't compile).
 
D

David Harmon

On Tue, 12 Sep 2006 08:32:12 -0400 in comp.lang.c++, "Victor
Bazarov said:
Huh? Since when does inheriting require virtual functions?

It doesn't require it; but it remains the most usual reason for
doing it. Indeed, there are classes whose main reason for existence
is to be inherited from without any virtual functions.

Looking back, the inheritance does indeed appear to be a useful
solution to the problem. But it could have benefited from a bit
more explanation along with it. I think it's the closest thing left
to the original request after eliminating ideas that don't work.

"Well, they're not really rules. Guidelines, more like."
-- Captain Barbarossa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top