Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using vector iterators with in a template class

I am attempting to create a vector iterator within a template class I am creating. The following is the trouble code.

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';
}

I am getting the following compile error:

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> > >*]()’ 

I have a feeling the compiler is getting confused with my typedef statement above but that is how I have seen to declare the correct iterator, but for some reason it is not working correctly. Any ideas?


1 Answers

If buffer is a std::vector< std::vector<T> > then buffer.begin() is a std::vector< std::vector<T> >::iterator or const_iterator, so your typedef doesn't match.

like image 178
juanchopanza Avatar answered Sep 03 '26 13:09

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!