Solved

Question about index operator and "at" function

Posted on 2004-09-10
2
224 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
ID: 12027894
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
ID: 12028349
To add to jkr's comment, don't forget to change your class declaration.

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

860 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