Templates and arrays of Nodes

This is a discussion on Templates and arrays of Nodes within the C++ Programming forums, part of the General Programming Boards category; Im making a tree type structure, it has 26 branches, each for a letter of the alphabet. i was wondering ...

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Templates and arrays of Nodes

    Im making a tree type structure, it has 26 branches, each for a letter of the alphabet. i was wondering how can i make an array of pointers to a Node struct.

    Code:
    template <class T>
    struct Nodes
    {
    ...
    };
    typedef struct Nodes Node;
    
    template <class T>
    class Foo
    {
      Node *list[26];
    public:
    ...
    };
    will this work?
    Last edited by Mastadex; 09-30-2005 at 02:40 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Node<T>* list[26];

  3. #3
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Are you sure? some of the tutorials i read dont have the <T> there.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> Are you sure?

    No, but if you compile the code with the <T> and again without, one works and one doesn't. You have to specify the template type of Node somehow, which is why the <T> should be in there.
    Code:
    template <class T>
    struct Node
    {
    };
    
    template <class T>
    class Foo
    {
      Node *list[26];
    public:
    };
    
    int main()
    {
        Foo<int> ifoo;
    }
    Code:
    error C2955: 'Node' : use of class template requires template argument list

  5. #5
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    how is the compiler going to know what type to make Node if you don't tell it to? You definitely need to use Node<T> maybe the book had a typo, you will learn that there are a lot of errors in technical books, more than you would imagine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  2. Dynamic Memory and Arrays
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2006, 04:09 AM
  3. Templates
    By arjunajay in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 10:17 AM
  4. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  5. Casting with character arrays
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2003, 09:47 PM

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