templates error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rupert harrison

    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(TNod e *&temp, DT *info);
    void Print_rec(TNode *);

    public:

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

    };

    static BSTree <DT> words;
    #endif



  • Kevin Goodsell

    #2
    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(TNod e *&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.

    Comment

    • Jerry Coffin

      #3
      Re: templates error

      In article <ge7gb.18613$pv 6.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 "environmen t" 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.

      Comment

      Working...