Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Question about index operator and "at" function

Posted on 2004-09-10
2
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

719 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