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
Solved

stack class using a templace vector class overloading [] problem

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

839 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