How to use typedef together with template ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VanKha
    New Member
    • May 2007
    • 21

    How to use typedef together with template ?

    I have some class,e.g
    Code:
    template<class T>
    class node{
      T key;
      node* next;
    };
    and I want to define a pointer to the class node as a type . How to declare ?
    I try typedef node<T>* nodeptr but it doesn't work,there'll be error .
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by VanKha
    I have some class,e.g
    Code:
    template<class T>
    class node{
      T key;
      node* next;
    };
    and I want to define a pointer to the class node as a type . How to declare ?
    I try typedef node<T>* nodeptr but it doesn't work,there'll be error .
    What is the error exactly? That error surprises me a bit ...

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      A typedef creates a new name for an existing type. A class template is not a type. Rather, it is a pattern for a type. Not until you specialize it can you typedef it:
      [code=cpp]
      typedef node<T>* nodeptr; //error
      typedef node<int>* nodeptr; //OK
      [/code]

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by weaknessforcats
        A typedef creates a new name for an existing type. A class template is not a type. Rather, it is a pattern for a type. Not until you specialize it can you typedef it:
        [code=cpp]
        typedef node<T>* nodeptr; //error
        typedef node<int>* nodeptr; //OK
        [/code]
        Darn, I missed this one completely; thanks for helping out.

        kind regards,

        Jos

        Comment

        • VanKha
          New Member
          • May 2007
          • 21

          #5
          Thanks a lot . But if I have to declare a specific T , another problem arises . It is because of the fact that I just compiled successfully a program with the class node and a class list using that "node" . I do like this

          class node...
          typedef node* nodeptr;
          class list
          //..using nodeptr many times here

          and it compiles well !
          Then I decide to use the template . And so , I must have node<T>* as nodeptr to make use of it in the list class . If I cannot declare nodeptr like the wrong syntax(!!!) , how can I write the list class ?Don't tell me that just go around the program and replace nodeptr with node<T>* ! Is there anyway to come around this problem ?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by VanKha
            Then I decide to use the template . And so , I must have node<T>* as nodeptr to make use of it in the list class .
            You mean like this:
            [code=cpp]
            template<class T>
            class node{
            T key;
            node* next;
            };


            template<class T>
            struct List
            {
            node<T>* start;
            node<T>* end;
            node<T>* current;
            };

            int main()
            {
            List<int> mylist;
            }
            [/code]

            This compile and links just fine.

            No typedef.
            Last edited by weaknessforcats; Aug 1 '07, 04:52 PM. Reason: fix bug in example code

            Comment

            • VanKha
              New Member
              • May 2007
              • 21

              #7
              Yes,but the thing is I have written nodeptr in my program instead of node<T>* ! I mean that at first I wrote like that and after updating with the template method pattern , I had to change all to node<T>* ? If the typedef could have worked , I have to do nothing but now that the typedef is wrong,I have to change all !! Sorry,just because I think that it's so unreasonable . Have anybody encounter the problem like me ? I think the answer must be yes,because just like me now,everybody must have the 'newbie in c++' period , so after writing the code without template,then reading textbooks,seein g the usefulness,then attemping to apply the template method,you'll expect that just change from typedef node* nodeptr to typdef node<T>* nodeptr and no more ! It's really natural expectation,isn 't it ?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                It's expected with a typedef that you already have a type. A template is not a type.

                Ultimately, you will need to change your code so it is acceptable to the compiler. You aren't the first one who had to ditch a lot of work over an assumption.

                The lesson here is to write only a little code and then compile. Once it compiles, then write a little more. That way you always have code that compiles and any errors will usually be in the code that was last added.

                Writing code for a long time and then doing a compile is a recipe for rework and frustration.

                Comment

                Working...