• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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.
0
mizizike
Asked:
mizizike
1 Solution
 
jkrCommented:
That should be more like

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)];
    }
}

0
 
AxterCommented:
To add to jkr's comment, don't forget to change your class declaration.

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now