代码之家  ›  专栏  ›  技术社区  ›  DavidG

带有自定义模板的STL迭代器

  •  4
  • DavidG  · 技术社区  · 17 年前

    我有以下模板方法,

    template <class T>
    void Class::setData( vector<T> data )
    {    
        vector<T>::iterator it;
    }
    

    我收到以下编译错误(XCode/gcc)

    错误:预期的;在“it”之前

    here (read down to see it's the same even though it starts out with a different issue) 但它们似乎已经通过更新Visual Studio得到了解决。这让我猜测这是一个编译器问题,应该编译,对吗?通过从0到size的索引进行迭代是可行的,但这不是我更喜欢实现此函数的方式。还有别的办法吗? 谢谢

    3 回复  |  直到 17 年前
        1
  •  10
  •   dirkgently    17 年前

    typename #include 有…的 vector iterator using namespace std; 在某个范围内。用途:

    typename vector<T>::iterator it;
    

    查找从属名称。开始 here .

        2
  •  1
  •   Paolo Tedesco    17 年前

    typename :

    #include <vector>
    using namespace std;
    
    class Class{
    public:
        template <class T>
        void setData( vector<T> data ) {
            typename vector<T>::iterator it;
        }
    };
    
        3
  •  0
  •   Douglas Leeder    17 年前

    尝试:

    template <class T>
    void Class::setData( std::vector<T> data )
    {    
        std::vector<T>::iterator it;
    }
    

    只是万一它失踪了 using 声明?