Solved

stack class using a templace vector class overloading [] problem

Posted on 2014-09-28
5
241 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

937 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

6 Experts available now in Live!

Get 1:1 Help Now