2012-04-13 75 views
3

我正在尝试在我创建的模板类中创建矢量迭代器。以下是故障代码。在模板类中使用矢量迭代器

void editor<T>::insert() 
{ 
     typedef typename std::vector<T>::const_iterator itr; 
     itr it; 
     it = this->buffer.begin(); 

     for(int i = 0; i < line_num -1; ++i) 
     { 
      ++it; 
     } 

     this->buffer.insert(it, user_text); 
     std::cout << "Cool, Your new line has been inserted." << '\n'; 
    } 
    std::cout << '\n'; 
} 

我收到以下编译错误:

error: no match for ‘operator=’ in ‘it = ((editor<std::basic_string<char> >*)this)->editor<std::basic_string<char> >::buffer.std::vector<_Tp, _Alloc>::begin [with _Tp = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >, _Alloc = std::allocator<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >*, std::vector<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >, std::allocator<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > > > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >*]()’ 

我有一种感觉,编译器感到困惑与我typedef上述声明,但是这就是我所看到的申报正确的迭代器,但由于某些原因,它不能正常工作。有任何想法吗?

+0

'buffer'的定义是什么? – Naveen 2012-04-13 05:26:59

+0

@Naveen缓冲区声明'std :: vector >缓冲区;'它是我的课程的multidim向量。 – 2012-04-13 05:28:14

+0

你可以发布调用这个函数的代码吗? – Naveen 2012-04-13 05:30:51

回答

7

如果bufferstd::vector< std::vector<T> >然后buffer.begin()std::vector< std::vector<T> >::iteratorconst_iterator,所以你typedef不匹配。

+0

如果'T'在这里是'std :: vector '? – Naveen 2012-04-13 05:33:13

+0

@Naveen:如果你看他的错误信息'T'是'std :: basic_string ' – 2012-04-13 05:34:33

+0

@Naveen我假设'T'在缓冲区定义和代码片段中是一样的。否则'缓冲区'将是一个'std :: vector >>'。 – juanchopanza 2012-04-13 05:43:09