Connecting Tech Pros Worldwide Help | Site Map

templates error

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:48 PM
Rupert harrison
Guest
 
Posts: n/a
Default templates error

Could someone tell me how to fix this error at the static BSTree <DT>
words
line. I get a type name expected and declaration terminated
incorectly error.

I've tried adding different data types but it only gets worse.


#ifndef BSTree_H
#define BSTree_H

template<class DT>
class BSTree
{
template <class DT>
class TNode
{
TNode *right;
TNode *left;

public:
DT *info;

friend class BSTree;
};

TNode *root;

void Insert_rec(TNode *&temp, DT *info);
void Print_rec(TNode *);

public:

void Insert(DT *);
void Print(void);

};

static BSTree <DT> words;
#endif




  #2  
Old July 19th, 2005, 06:48 PM
Kevin Goodsell
Guest
 
Posts: n/a
Default Re: templates error

Rupert harrison wrote:
[color=blue]
> Could someone tell me how to fix this error at the static BSTree <DT>
> words
line. I get a type name expected and declaration terminated
> incorectly error.
>
> I've tried adding different data types but it only gets worse.
>
>
> #ifndef BSTree_H
> #define BSTree_H
>
> template<class DT>
> class BSTree
> {
> template <class DT>
> class TNode
> {
> TNode *right;
> TNode *left;
>
> public:
> DT *info;
>
> friend class BSTree;
> };
>
> TNode *root;
>
> void Insert_rec(TNode *&temp, DT *info);
> void Print_rec(TNode *);
>
> public:
>
> void Insert(DT *);
> void Print(void);
>
> };
>
> static BSTree <DT> words;[/color]

DT does not appear to be a real type. Are you sure you didn't want
std::string or something like that?

Also, placing this here and declaring it static seems suspicious. It
means each source file you #include this header in will have its own,
different BSTree called 'words'. That seems unlikely to be what you
want. Also, this use of static is deprecated in favor of anonymous
namespaces.

My guess is that you should replace 'static' with 'extern' and create
the actual object in a .cpp file somewhere.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

  #3  
Old July 19th, 2005, 06:55 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: templates error

In article <ge7gb.18613$pv6.10579@twister.nyc.rr.com>, "Rupert
harrison" <Wu Tang@nyc.rr.com> says...

[ ... ]
[color=blue]
> #ifndef BSTree_H
> #define BSTree_H[/color]

This is normally used in headers, a point that will become important
below.
[color=blue]
> template<class DT>
> class BSTree[/color]

This means that from here to the end of the template declaration, "DT"
will refer to whatever type is used to instantiate the template.
[color=blue]
> template <class DT>
> class TNode
> {
> TNode *right;
> TNode *left;
>
> public:
> DT *info;
>
> friend class BSTree;
> };[/color]

At least if I understand what you're doing, making this into a separate
template isn't needed. It would be necessary if and only if you wanted
to support instantiating a TNode over a different type than its
surrounding BSTree was being instantiated over. From the looks of
things, that's not the case though -- a BSTree<X> implies TNode<x> as
well. In that case, you can simplify TNode a bit:

class TNode {
TNode *left, *right;
public:
DT *info;
friend class BSTree;
};
[color=blue]
> };[/color]

This is the end of the template declaration -- at this point, "DT"
ceases to mean anything (unless something else has declared it has
having a meaning. Given that this is a header, you (normally) don't
want to assume that it means anything beyond this point (i.e. a header
normally shouldn't assume anything about the "environment" into which
it's included.
[color=blue]
> static BSTree <DT> words;[/color]

That means at this point, "BSTree<DT>" is meaningless -- if you want to
instantiate BSTree, you have to supply _some_ real type.

Another point about headers, however, is that they should normally
declare types, but NOT define objects. I consider it highly doubtful
that this should be here at all. Perhaps you can tell us what this is
intended to accomplish, and we can help you toward accomplishing it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.