473,320 Members | 1,707 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,320 software developers and data experts.

How to write this recursive definition?

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<PairTrieNode;
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!

Sep 8 '06 #1
10 2519
Moonlit wrote:
<lk*****@gmail.comwrote:
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<PairTrieNode;
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?
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


Thanks very much!
Sep 8 '06 #2

lk*****@gmail.com wrote:
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<PairTrieNode;
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<PairTrieNode;
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
Thanks very much!
Sep 8 '06 #3
How about this:

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

Sep 8 '06 #4
Oops your right I didn't examine the code to carefully :-(

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Thomas Tutone" <Th***********@yahoo.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
Moonlit wrote:
><lk*****@gmail.comwrote:
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<PairTrieNode;
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?
>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

>
Thanks very much!

Sep 8 '06 #5

Michael wrote:
How about this:

struct TrieNode : public vector< pair<char, struct TrieNode*
{
};
inheriting from std::vector is usually a bad idea...

Sep 11 '06 #6
Radu wrote:
Michael wrote:
>How about this:

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

inheriting from std::vector is usually a bad idea...
Why?
Sep 12 '06 #7
On Mon, 11 Sep 2006 09:31:20 -0400 in comp.lang.c++, "Victor
Bazarov" <v.********@comAcast.netwrote,
>>
inheriting from std::vector is usually a bad idea...

Why?
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.

Sep 12 '06 #8
David Harmon wrote:
On Mon, 11 Sep 2006 09:31:20 -0400 in comp.lang.c++, "Victor
Bazarov" <v.********@comAcast.netwrote,
>>>
inheriting from std::vector is usually a bad idea...

Why?

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '06 #9

Victor Bazarov wrote:
David Harmon wrote:
On Mon, 11 Sep 2006 09:31:20 -0400 in comp.lang.c++, "Victor
Bazarov" <v.********@comAcast.netwrote,
>>
inheriting from std::vector is usually a bad idea...

Why?
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.
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, "fuck 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).

Sep 12 '06 #10
On Tue, 12 Sep 2006 08:32:12 -0400 in comp.lang.c++, "Victor
Bazarov" <v.********@comAcast.netwrote,
>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?
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

Sep 12 '06 #11

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

Similar topics

3
by: Michael Foord | last post by:
I've been using the excellent XMLObject and have unfortunately come up against what *looks* to me like a bug - although it's very possible that the problem is mine !! I want to use XML object...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
11
by: Alexander Stippler | last post by:
Hi, I have a little problem to design some template classes in a realizable way. I have some Container classes and some Proxy classes (proxies for elements). Looks like this: // P is the...
37
by: Protoman | last post by:
Hi!!! Protoman here, I need to write an infinite precision number class b/c I want to compute pi. Give me some sample code. Also, when I run my program and it computes pi, will my computer freeze...
9
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
6
by: hurcan solter | last post by:
I am generating a bunch of class type PODs from an xml file.The thing is,xml file has no notion of order of definitions, so I end up in situations like; struct a { b bval; };
5
by: Peng Yu | last post by:
Hi, I want a macro #expand that can be expanded to one of the following depending on its argument f(i1) f(i1, i2) f(i1, i2, i3) ...
1
by: bipinskulkarni | last post by:
How to write recursive stored procedure in mysql?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.