【问题标题】:Scope Resolution::Nested Template Typename范围解析::嵌套模板类型名
【发布时间】:2011-04-06 17:11:37
【问题描述】:

我的重载前缀运算符在查找范围时遇到问题。

#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;

template <typename T>
struct node
{
   T data;
   node* next;
   node* prev;
};

template <typename T>
class list
{
   public:
      class iterator
      {
         public:
            iterator() : inode(0) {}
            iterator(node<T>* current) : inode(current) {}

            // overload prefix increment operator (++x)
            iterator& operator ++();
            }    
         private:
            node<T>* inode; // 
      };

      iterator begin();
      iterator end();

   private:
      // pointer to the head of the dclist
      node<T>* head;
};

// constructor
template <typename T>
list<T>::list() {
   head = new node<T>;
   head->next = head;
   head->prev = head;
}

template <typename T> // This function needs proper scope resolution
typename list<T>::iterator::iterator& iterator<T>::operator ++() {
   inode = inode->next;
   return this;
}

// return iterate from beginning //
template <typename T>
typename list<T>::iterator list<T>::begin() { 
   return list<T>::iterator(head->next);
}

// return iterate to end // 
template <typename T>
typename list<T>::iterator list<T>::end() {
   return list<T>::iterator(head);
}

#endif

【问题讨论】:

    标签: c++ templates scope operator-overloading


    【解决方案1】:

    函数名没有正确限定,返回类型多了一个iterator

    template <typename T>
    typename list<T>::iterator::iterator& iterator<T>::operator ++()
    

    这需要:

    template <typename T>
    typename list<T>::iterator& list<T>::iterator::operator++()
    

    还请注意,使用using namespace std; 并使用与标准库名称相同的名称(listiterator 等)声明您自己的类型只能以泪水告终。你应该放弃using namespace std;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 2017-11-30
      • 2011-09-28
      • 1970-01-01
      • 2021-09-14
      • 2013-07-04
      • 2011-03-19
      相关资源
      最近更新 更多