Question about class templates

I'm currently rewriting a class definition (with it's member functions) to be a class template. To summarize the program, I am writing my own stack class in which my assignment originally had us build the stack class to only accept ints as the data type. Needless to say, now we are modifying it to allow for any data type to be stored.

My question isn't necessarily a problem with my code as I have tested it and it seems to work flawlessly with the test code we were given for the first phase of this assignment. What I am wondering is if there is a simpler way for me to do my member functions; what I found when modifying this program was that for every single member function I was having to include

1
2
  template <class T>
  return_type class <data_type> :: functionName(function parameters)


The same was true for my overloaded operators, for example my assignment operator looks like this right now:

1
2
3
4
5
template <class T>
Stack <T> & Stack <T> :: operator = (const Stack & rhs)
{
   stuff
}


Was I correct in my approach of this, or is there a simpler way to kill several birds with one stone here? It sure seems like there should be...
Last edited on
you could define your member functions inside the class template
1
2
3
4
5
6
7
8
template <class T>
class Stack {
 // other stuff
    Stack& operator=(const Stack& rhs)
   {
      // stuff
   }
};
That is an option, but I think my teacher wants only prototypes within the class and no actual declarations. Oh well, my code works, I was just wondering if I had missed something obvious
Topic archived. No new replies allowed.