Solved

Question about index operator and "at" function

Posted on 2004-09-10
2
221 Views
Last Modified: 2010-04-01
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
Comment
Question by:mizizike
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
To add to jkr's comment, don't forget to change your class declaration.

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now