Solved

stack class using a templace vector class overloading [] problem

Posted on 2014-09-28
5
234 Views
Last Modified: 2014-09-29
I have built a vector template class and I am now building a stack class that uses the vector class.

When I overload the [] operator I am getting a cannot convert int to vector <T> c2440
definiton of {} are

      Vector<T> & operator[](int index);
      const Vector<T> & operator[](int index) const;

func is
template<typename T>Vector<T> & Vector<T>::operator[](int index) // Returns an object at location index without bounds checking
{
      return vec_array[index];
}

template<typename T>const Vector<T> & Vector<T>::operator[](int index) const // Returns an object at location index without bounds checking
{
      return vec_array[index];
}

Stack is
template <class T>
class stack: Vector <T>
{
  private:
      Vector<T> stack_array;

instantiated using

 stack<int> Stack1;
0
Comment
Question by:pcomb
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40348838
Could you post the complete code? These snippets alone aren't really insightful...
0
 

Author Comment

by:pcomb
ID: 40348846
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40348891
Ah, I think I got it - you had your accessors all in the form 'Base<T> operator[](int index);' where you need them to be like 'T operator[](int index);'. Try the following (also check for '<--' which I used to mark other issues):
base.h
pile.h
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40348990
I noticed the T vs. Base<T> issue. But then I figured that the author must have tested his base class thoroughly before trying to use it, and concluded that I must have skimmed over the base class too fast.

Could you post your Base class tests (i.e., the test.cpp file that thoroughly verifies your base class.

In general, when you use a user defined type, the assumption is that this UDT has been tested.
0
 

Author Closing Comment

by:pcomb
ID: 40349000
perfect many thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

706 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

18 Experts available now in Live!

Get 1:1 Help Now