Simple Question - Double Linked List in a Template

So I'm writing this toolkit to provide functionality for a double linked list inside of a template. The toolkit functions are mostly straightforward (at least so far). I have this really simple question, though. When I am programming from main() and want to start a new list, how do I create the very first item in a list?

Here is my constructor:

1
2
3
4
5
6
7
8
9
10
11
template <class Item>
class D_Node 
{ 
public: 
   D_Node( const Item &init_data = Item( ), 
      D_Node *init_fore = NULL, D_Node *init_back = NULL )
      {
         data_field = init_data;
         link_fore = init_fore;
         link_back = init_back;
      };


So, say that I want to create a node with the integer 1 as its data_field. I thought that I would just write:

D_node my_node(1);

with the constructor filling in the NULL values for the pointers, but this doesn't compile. I get two errors:

missing template arguments before "my_node"
-and-
expected ';' before "my_node"

Note that my constructor sits within a template in a separate file (file: dnode.h).

Can anybody help. I'm missing some syntax here, but I don't know what it is.

Thanks!
Last edited on
The type of my_node in this case is D_node<int>, not D_node.

 
D_Node<int> my_node( 1 );

Thanks, that worked. In the book that I am studying, it says that when a template function is used, the compiler examines the types of arguments and automatically determines the type for Item.

Is there a better way than how I am creating this initial node? The method that I am using apparently means that I have to tell the compiler what the variable type is, which might be a hint that this isn't best method???

Just kicking the tires to figure this whole template thing out.

Thanks again!
In your case, you have no template functions, you have a template class. The compiler's auto type deduction does not apply in this case.

Is there a better way? Dunno. Depends on what you define as "better". For example, std::string is actually a typedef of the std::basic_string class instantiated on a char. You could do something like:

1
2
3
typedef D_Node<int> IntNode;

IntNode node( 1 );

Topic archived. No new replies allowed.