Template Function Call

Jul 30, 2011 at 9:41pm
I'm having trouble getting this function call straightened out. It's part of a doubly linked list set up in a template. I'm getting a "no matching function call" error on the last line in main(). Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <iostream>
#include <string>
#include "dnode.h"
using namespace std;

int main()
{
    D_Node<int> first_node(3, NULL, NULL);
    D_Node<int> *head_ptr;
    D_Node<int> *tail_ptr;

    head_ptr = &first_node;
    tail_ptr = &first_node;

    D_Node<int> *previous_ptr;
    previous_ptr = new D_Node<int>;
    previous_ptr = dlist_search(head_ptr, 3);
}


in dnode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   template <class Item>
   class D_Node
   {
   public:
   // TYPEDEF
   typedef Item value_type;
   // CONSTRUCTOR
   D_Node( const Item &init_data = Item( ),
      D_Node *init_fore = NULL, D_Node *init_back = NULL );

   // MODIFICATION MEMBER FUNCTIONS to set the data and link fields:---------
   void set_data( const Item& new_data);
   void set_fore( D_Node *new_fore);
   void set_back( D_Node *new_back);

   // CONSTANT MEMBER FUNCTIONS to retrieve member variables----------------
   Item data( ) const;

   const D_Node *fore( ) const;
   D_Node *fore( );

   const D_Node *back( ) const;
   D_Node *back( );

   private:
   Item data_field;
   D_Node *link_fore;
   D_Node *link_back;
};

template <class Item>
D_Node<Item> dlist_search(D_Node<Item> *&head_ptr, Item &target);

#include "dnode.template" 


in dnode.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "dnode.h"
#include <assert.h>

template <class Item>
D_Node<Item> dlist_search(D_Node<Item> *&head_ptr, Item &target)
{
   D_Node<Item> *cursor;

   for (cursor = head_ptr; cursor != NULL; cursor = cursor->fore())
   {
     if (target == cursor->data_field())
     {
         return cursor;
     }
   }
   return NULL;
}


Obviously, there is a lot more code, but I think this is what is relevant.

Can anybody see why I am not able to call this function? Thanks!
Jul 30, 2011 at 9:43pm
closed account (zb0S216C)
Any function that returns a D_Node requires a type-specifier. For example:

 
const D_Node< Item > *fore( ) const;

Wazzak
Last edited on Jul 30, 2011 at 9:43pm
Jul 30, 2011 at 9:56pm
Thanks. However, I don't understand how to implement this advice. How does this change my function call of:

previous_ptr = dlist_search(head_ptr, 3);

I appreciate your help.
Jul 30, 2011 at 10:05pm
closed account (zb0S216C)
Placing < int > after the function identifier and before the parameter list may solve your problem. For example:

previous_ptr = dlist_search< int >(head_ptr, 3);

Some compilers may know the type it's working with automatically, but yours may not.

Wazzak
Last edited on Jul 30, 2011 at 10:05pm
Jul 30, 2011 at 10:06pm
Actually, I think that it changes the function prototype, but I can't figure out the syntax.
Jul 30, 2011 at 10:18pm
I see at least two things that need to be fixed.

(1) Your function's return type. It's D_Node<Item> while it should be D_Node<Item> *

(2) The second argument to your function. It should be const Item & target

    (a) since you can't bind a literal constant (the number '3') to a non - const reference and

    (b) if you want to be const correct, since your function doesn't modify 'target' anyway.

Also, this line -> previous_ptr = new D_Node<int>; is unnecessary,
but it won't cause you any trouble other than a minor memory leak.
Last edited on Jul 30, 2011 at 10:32pm
Jul 30, 2011 at 10:27pm
It works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I can't tell you how much time I have spent on this and how much I appreciate your help.

Thanks a million!
Topic archived. No new replies allowed.