help with template class using a template node

This is a discussion on help with template class using a template node within the C++ Programming forums, part of the General Programming Boards category; I have a template node class that I have been using in some projects for creating linked lists with. Now ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    help with template class using a template node

    I have a template node class that I have been using in some projects for creating linked lists with. Now I am trying to create a template class for working with sets. For some reason tho when I try to declare my node pointer in the template set class it is giving me an error saying that it cannot declare node without a type. Here is the code:

    Node class
    Code:
    template <class T>
    class node{
    
      public:
             node(){linkfield = NULL;}
             node(T d, node *lf){datafield = d; linkfield = lf;}
      
      T& data(){return datafield;}
      node * link(){return linkfield;}
    
      void setData(T d){datafield = d;}
      void setLink(node * lf){linkfield = lf;}
       
     private: 
               T datafield;
                node * linkfield;
    };
    Set class

    Code:
    template <class T>
    class Set{
    
      public:
             Set();
             Set(const Set& other);
             ~Set();
             
      bool is_element(T d);
      bool is_subset(const Set<T> other);
      void insert(T d);
      void input();
      void output();
      Set unian(const Set<T> other);
      Set intersect(const Set<T> other);
      
      void operator =(const Set<T>& other);
       
     private: 
              node <T> * head;
    };
    Besides the node template class I haven't worked with templates at all so I'm not sure if this is some little syntax error or a bigger problem in calling a template class from a template class.
    Thanks
    -alex

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Looks okay to me. What compiler, and can you post more code? Something short and complete that we can compile right after a cut and paste to see what you're seeing?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    Here us set.template which isnt done yet, I was just trying to see if my constructors and such worked before moving on. Using g++ compiler/minGW with dev-cpp. The end result will be compiled with g++ on a solaris system.

    set.template
    Code:
    #include "node.h"
    using namespace std;
    
    //CONSTRUCTOR/DESCTRUCTOR/COPY CONSTRUCTOR
    
    //constructor for new set
    template <class T>
    Set<T>::Set()
    {
       head = NULL;
    }
    
    //destructor for set
    template <class T>
    Set<T>::~Set()
    {
       cout<<"Destructor called.\n";
       while(head != NULL)
       {
          node <T> * temp;
          temp = head;
          head->setLink(head->link());      
          delete temp;
       }
    }
    
    //copy constructor for set
    template <class T>
    Set<T>::Set(const Set<T>& other)
    {
       if (other.head == NULL)
          head = NULL;
       else
       {
          node <T> * tmp = other.head;
          node <T> * end = new node<T>();
          
          end->setData(tmp->data());
          
          head = end;
          tmp->setLink(tmp->link());
          while(tmp != NULL)
          {
             end->setLink(new node<T>());
             end = end->link();
             end->setData(tmp->data());         
             tmp = tmp->link();
          }
          end->setLink(NULL);
       }
    
    }
    here is my main where I was just trying to create a new Set item to see if it worked then i made a simple If to test the copy and to let the new item go out of scope to test desctructor.

    main.cc
    Code:
    #include "set.h"
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
    
    Set <char> newSet();
    if(true){
             Set <char> setTwo(newSet);
             cout <<"Set two created from copy of newSet.\n";
             }
    
    return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, this is an error:
    Code:
    Set <char> newSet();
    You're declaring a function, not defining an object. Lose the parens to declare an object:
    Code:
    Set <char> newSet;
    Aside from that, all seems well as long as you define NULL somewhere.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    it is still saying 'ISO C++ forbids declaration of node with no type' , 'expected ; before < token' for this line in set.h:

    Code:
              node <T> * head;
    any ideas?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Code:
    typename node<T> *head
    More details:
    http://www.gotw.ca/gotw/035.htm

    gg

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >typename node<T> *head
    If the compiler requires typename in this context, it's broken.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    yea that isnt the problem, i used the same node.h in another project and declared nodes the same way except instead of <T> i used other objects I had made like <Resident> and it worked fine. Any other suggestions? Thanks

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    template <class T>
    class node{

    public:
    node(){linkfield = NULL;}
    node(T d, node *lf){datafield = d; linkfield = lf;}

    T& data(){return datafield;}
    node * link(){return linkfield;}

    void setData(T d){datafield = d;}
    void setLink(node * lf){linkfield = lf;}

    private:
    T datafield;
    node * linkfield;
    };
    I think your code is in error here because "node*" isn't a full type. You ought to be using node<T> here for all cases where you use "node". The compiler does this because in some cases you'll want
    Code:
    template<typename T>
    struct Node {
           Node<int>* integerNode;
    };
    Doesn't happen often, but C++ remaims flexible in this regard.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    so the new code would look like this:

    Code:
    template <class T>
    class node{
    
      public:
             node<T>(){linkfield = NULL;}
             node<T>(T d, node<T> *lf){datafield = d; linkfield = lf;}
      
      T& data(){return datafield;}
      node<T> * link(){return linkfield;}
    
      void setData(T d){datafield = d;}
      void setLink(node<T> * lf){linkfield = lf;}
       
     private: 
               T datafield;
                node<T> * linkfield;
    };
    I'm not sure if thats what you meant but i tried it out and am still getting same error. Just can't get the declaration of head to compile no matter what...

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You ought to be using node<T> here for all cases where you use "node"
    Inside the definition of a template class, the bare name of the template class is always understood to be dependent on the template parameters. So 'node' is understood to be 'node<T>' without fail while inside the definition. The existing class is correct.

    >am still getting same error
    First things first. Lump everything together into one file and make sure you still get the error. If you do then it's a code issue and not a file structure issue. Then pare down the code until it's as small as possible and still gives you the error. Then post it here. Looking at snippets is obviously not helping us to help you, and it's bringing out the guessers.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Inside the definition of a template class, the bare name of the template class is always understood to be dependent on the template parameters. So 'node' is understood to be 'node<T>' without fail while inside the definition. The existing class is correct.
    Yes, you must be right here. By habit I always will use Node<T> but I'll leave out its use in the constructor's name. Anyway, the combined node class and Set class compiles on a solaris g++ system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21