-2

I am trying to override the [] operator of vector to use its at() function instead so it checks for the bounds too. However, it gives me the error: invalid use of incomplete type 'class std::vector<_Tp>'

template <typename T> inline T& vector<T>::operator [] (vector<T>& v, size_t s) {
    return  v.at(s);
}
ljyip
  • 41
  • 5

1 Answers1

1

You cannot do that. operator[] can be overloaded as a member function only.

See https://en.cppreference.com/w/cpp/language/operators#Overloaded_operators for more on the subject.

R Sahu
  • 204,454
  • 14
  • 159
  • 270