C++ Template syntax question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KiddoGuy
    New Member
    • Nov 2008
    • 17

    #1

    C++ Template syntax question

    Hello, I have an "abstract" template header file that I am extending with another header file. I want to implement the methods from the abstract class in the extending class header file. One is called List and the other is called LinkedList, which is the implementation of List. What should the method headers look like for, say, an add method that takes a generic parameter?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    something like

    Code:
    template<class T> void Add(T &newNode);

    Comment

    • KiddoGuy
      New Member
      • Nov 2008
      • 17

      #3
      Shouldn't I need class names somewhere in there?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        For the definition of the function yes just before the function name but not for the declaration of the function in the class.

        Comment

        • KiddoGuy
          New Member
          • Nov 2008
          • 17

          #5
          Ok, thanks. So if i am including the header I am extending and publicly extend that class by doing something like
          Code:
          template class<T>
          class LinkedList: public List
          should that be fine? Do I need to declare a generic type on List? I'm sorry... I'm fluent in Java and C but still learning nuances of C++ and writing my first program using templates... kind of jumped in the deep end.

          The problem so far is I'm getting a compiler error "expected class-name before ‘{’ token" and I don't know where that's coming from.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            template <class T>

            Comment

            Working...