0

I'm trying to compile the following code:

template<typename T> void get_sub_vector(const std::vector<T>& vec, int begin_index, int end_index, std::vector<T>& dst) {
    std::vector<T>::const_iterator first = vec.begin() + begin_index;
    std::vector<T>::const_iterator last = vec.begin() + end_index - 1;
    dst = std::vector<T>(first, last);
}

The code purpose is to get:

  • source std::vector of any type
  • destination std::vector of the same type
  • begin index and end index

Then it should copy all the values from the source vector, between the defined indices, into the destination vector.

I get the following compilation error:

error: need `typename` before `std::vector<T>::const_iterator` because `std::vector<T>` is a dependent scope

Any idea what I'm doing wrong with the templates syntax?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126

0 Answers0