typename和编译器的恩怨情仇

 这几天在写一个红黑树的时候,突然发现模板类中的使用了STL容器的待定类型在编译时报错

warning C4346: “std::vector<_Ty>::iterator” : 依赖名称不是类型
        用“typename”为前缀来表示类型
        c:/treecode/RedBlackTree/RedBlackTree.h(153) : 参见对正在编译的类模板实例化“RedBlackTree<T>”的引用
c:/treecode/RedBlackTree/RedBlackTree.h(60) : error C2146: 语法错误 : 缺少“;”(在标识符“__vector_iterator”的前面)
c:/treecode/RedBlackTree/RedBlackTree.h(60) : error C2501: “RedBlackTree<T>::__vector_iterator” : 缺少存储类或类型说明符

下面是源码

template <typename T>
struct __node                      // this structure represents a RED BLACK Tree- node
{
   T key;                          // key of the current node
   struct __node<T> *left;         // pointer to the left node
   struct __node<T> *right;        // pointer to the right node
   struct __node<T> *parent;       // pointer to the parent node
   int color;                      // color of the current RED BLACK Tree node - either 1:BLACK, 2:RED
};

template <typename T>
class RedBlackTree;                // forward declaration required to declare the type of pointers in bqueue

enum {BLACK=1,RED=2};              // color of a node of the RED BLACK Tree- can either be BALCK=1 or RED=2

// RED BLACK Tree class, used for maintaining the RED BLACK Tree entered by the user
// it contains member functions for performing insertion, deletion and and various tree
// traversal functions such as - preorder, inorder and post order tree traversal
// algorithms, as well as graph search algorithms such as - depth first search and
// breadth first search

template <typename T>
class RedBlackTree {

   public:

       // list of all typedefs goes here

       typedef __node<T>   rb_node;                                    // all RED BLACK TREE nodes will have this type of node
       typedef __node<T>** rbtree_node;                                // some functions will return a pointer of this type
       typedef std::vector<T>::iterator __vector_iterator;             // define a vector iterator, used by the sort functionality
       //typedef typename std::iterator_traits<IterT>::value_type value_type;

       // initial list of constructors goes here

       RedBlackTree () {}                                              // default constructor, this won't be used
       RedBlackTree (const T&);                                        // constructor which will be used for inserting nodes

       // the following two member functions are used for inserting new nodes into
       // the already created Red Black Tree, after the insertion the _insert_fixUp
       // function will be called which will balance the RED BLACK Tree

       rbtree_node  insert (const T&);                                 // insert a new node, returns a pointer to the root, this is the wrapper
       rbtree_node  __insert__fixUp (rb_node*);                        // after insertion, balance the Red Black Tree, returns a pointer to the root
       void         __insert (rb_node *);                              // function to insert a new node, present inside the wrapper - insert()

       // the following two functions will be used for performing right rotations and
       // left rotations in a Red Black Tree, for details of these functions please
       // check the chapter on Red Black Trees in CLRS

       rb_node* rotate_right (rb_node*);                               // rotate the current node right with its left child
       rb_node* rotate_left  (rb_node*);                               // rotate the current node left with its right child

       // find is the member function in the Red Black Tree that performs a binary
       // search of an item in O(log n) time

       rbtree_node   find (const T&);                                  // find a given item in the Red Black Tree, this is the wrapper
       rb_node*      __find (const T&);                                // recursively search the Red Black Tree to find a node with a given key
       rb_node*      __search__subtree (rb_node*,const T&);            // recursively search in the left/right subtree of the current node

       // the following functions will be used for recursive traversal of the Red
       // Black Tree- preorder, inorder and postorder, however these are just the
       // wrapper functions with the actual implementations of the traversal being
       // done inside __preorder,__inorder and __postorder functions

       void preorder  ();
       void inorder   ();
       void postorder ();

       // actual traversal functions for - preorder, inorder and postorder traversal

       rbtree_node __preorder  (rb_node*);                             // recursive preorder Red Black Tree traversal function
       rbtree_node __inorder   (rb_node*);                             // recursive inorder Red Black Tree traversal function
       rbtree_node __postorder (rb_node*);                             // recursive postorder Red Black Tree traversal function

       // adjacent_find (x) will return a pair which will which will have the predecessor
       // and the successor of x, this is the wrapper function to call __adjacent_find

       std::pair<T,T> adjacent_find (T);

       // the __adjacent_find will travel the Red Black Tree in an inorder fashion and
       // populate the vector __linear_vector. this vector will be searched afterwards
       // to find the position of the number entered. if the number is found then the
       // number just before it and the number just next to it are returned

       void __inorder_populate (std::vector<T>&);

       // the following function is the alternate version of the inorder function where
       // we keep on populating a vector v as and when we travel the Red Black Tree in
       // inorder fashion

       rbtree_node __inorder (rb_node*,std::vector<T>&);               // alternative implementation of the inorder functionality

       // erase the entire Red Black Tree in postorder fashion. in this method, we will
       // first check to see if the left and the right child of a given node is NULL or
       // not, in case both of them are NULL, then delete the node that is currently
       // being processed (the first erase function is the wrapper function)

       void* erase ();                                                 // this is the wrapper function erase
       void  __erase ();                                               // function for erasing the Red Black Tree

       // travel the Red Black Tree in postorder fashion and erase the node travelled
       // in this way if both its children (left and right) are NULL (have been erased)

       rb_node* __postorder_erase (rb_node*);

       // the following function will be used to sort the elements present in the Red
       // Black Tree in the comparison order in which they are entered

       std::pair<__vector_iterator,__vector_iterator> sort ();         // this is the wrapper function for the sort functionality
       std::vector<T>* __sort ();                                      // function to sort the elements of the Red Black Tree
       void sort_display (__vector_iterator,__vector_iterator);        // display the sorted list of Red Black Tree numbers

   protected:

       // the initialization list of all switches goes here

       bool initialize;            // this switch is not used presently

   private:

       rb_node **root;             // pointer to the root of the Red Black Tree, this is a reference pointer
       rb_node node;               // actual Red Black Tree node where all the data will be stored
};

 

后来找到了解决方法,原来编译器不能确定 std::vector<T>::iterator是什么,我们必须告诉编译器

它是一个“类型”

typedef typename std::vector<T>::iterator __vector_iterator;

就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值