Link to home
Start Free TrialLog in
Avatar of mizizike
mizizike

asked on

Question about index operator and "at" function

Hello. I am still working on a vector class and I have run into two problems. One that is of most importance is my index operator. The prototype looks like this:

//Index Operator
T& operator[unsigned int];

The implementation looks like this:

template <typename T>
T& vector<T>::operator[unsigned int pos]
{
      if(pos < THE_ARRAY_CAPACITY)
      {
            return theDataPtr[pos];
      }
      else
      {
            return theDataPtr[(THE_ARRAY_CAPACITY-1)];
      }
}

I get multiple errors with this function, and I do not know why. Most of them are along the lines of: operator expected '[' instead of '[]'. Then, when I try to do my at function, which looks like this:

Returns elements at position pos
T& at(unsigned int);

The implementation is exactly the same as the implementation for that of the index operator function, I get unresolved external operators and one that says vector<T>.at(pos) is a bunch of garbage. I require assistance in this matter and appreciate any insight given to me.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To add to jkr's comment, don't forget to change your class declaration.

//Index Operator
T& operator[](unsigned int pos);