Linked List functions

Hi again! I have a problem with the parameters of two of my functions:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
typedef List<int> Dlist;
template<class T> 
     Dlist concat(Dlist L1, Dlist L2)
       {
         Dlist L;
         elem<T> *temp1, *temp2;
         temp1 = L1.Start_ptr;
         temp2 = L1.Start_ptr; 
         while(temp1)
           {
             L.Add_Node(temp1->data);
             temp1 = temp1->next;          
           }
          while(temp2)
           {
             L.Add_Node(temp2->data);
             temp2 = temp2->next;          
           } 
          return L;  
       }
       
     template<class T> 
     T accumulate(T (*op)(T, T), T null_val, List<T>& L)
       {
         T s = null_val;
         elem<T> *temp = L.Start_ptr();
         while(temp)
           {
             s = concat(s, temp->data);
             temp = temp->next;         
           }
         return s;
       }

    int main()
      {         
        List<Dlist> A;
        Dlist B, C;
        
        B.Add_Node(1);
        B.Add_Node(2);
        B.Add_Node(3);
        
        C.Add_Node(4);
        C.Add_Node(5);
        C.Add_Node(6);
        
        A.Add_Node(B);
        A.Add_Node(C);
        
        Dlist New;
        Dlist L = accumulate(concat, New, A);
 
        cout << endl;
        system("pause");
        return 0;        
      }


Here are the errors:
no matches converting function `concat' to type `class Dlist (*)(class List<int>, class List<int>)'
error candidates are: template<class T> Dlist concat(Dlist, Dlist)


no matching function for call to `concat(Dlist&, Dlist&)'


I can't understand what the compiler is trying to tell me. Can somebody help me please?
Last edited on
Ok... so I cleared the second error and now only this occurs:
no matches converting function `concat' to type `class Dlist (*)(class List<int>, class List<int>)'


Any ideas how I can debug it?
I suspect it's line 29? Right? You should include that in the error as well; it helps greatly.

As I see it you're trying to make the function return a pointer to Dlist instead of a Dlist object; your use of the function is wrong.
In fact it is line 52.
To begin with, there is no way for the compiler to deduce the type of T you intend to use for the template function concat (and, indeed, one wonders why you're making it a template function.)

I might expect concat to look more like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
T concat( T a, T b)
{
    // since a and b are copies of lists, we can just append b to a and return a.
    auto node = b.Start_ptr ;

    while ( node )
    {
        a.Add_node(node->data) ;
        node = node->next ;
    }

    return a ;
}


and then the call becomes: Dlist L = accumulate(concat<Dlist>, New, A);

[edit: This all assuming you have supplied an appropriate copy constructor and copy assignment operator, of course.]
Last edited on
Topic archived. No new replies allowed.