-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
  • 21
  • 3

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
  • 200,579
  • 13
  • 144
  • 260