Passing function pointer (error C3867)

Hello,

I am reading values from a file then inserting them into a doublylinked list. The function is supose to insert the values in an ascending order. Right now I am getting some values stored but not all. I have tried to find info and have even drawn some pictures but cant seem to get the logic right. Below is my function, input file and results or insert.

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
58
template <typename T>
Error_code DoublyList<T>::Insert(const T &val)
{
	Error_code outcome = success;

	Node *newNode, *nodePtr;
	newNode = new Node;
	newNode->value = val;

    // Insertion into an Empty List.
    if(empty())
    {
       head = newNode;
       last = newNode;
       newNode->prev = NULL;
       newNode->next = NULL;
	   count++;
	   return outcome;
    }
    else
    {
       nodePtr = head;
       while( val > nodePtr->value && nodePtr->next != last->next)
		   nodePtr = nodePtr->next;

       if(nodePtr == head)
       {
         newNode->prev = nodePtr;
         newNode->next = NULL;
         head->next = newNode;
         last = newNode;
		 count++;
		 return outcome;
       }
       else
       {
		   if(nodePtr == last && val > last->value)
		   {
			 last->next = new Node;
			 (last->next)->prev = last;
			 last = last->next;
			 last->next = NULL;
			 last->value = val;
			 count++;
			 return outcome;
		   }
		   else
		   {
			 newNode->next = nodePtr;
			 (nodePtr->prev)->next = newNode;
			 newNode->prev = nodePtr->prev;
			 nodePtr->prev = newNode;
			 count++;
			 return outcome;
		   }
	   }
	}
}


INPUT FILE:
15
831
633
816
85
738
771
720
710
296
266
276
429
480
414
846
333
590
748
878
647
263
862
481
720
198
175
227
7
166
660
547
36
314
575
520
236

What actually got stored:
15
7
36
166
236
236
314
520
547
575
660
Last edited on
Simplified the insert function and got it working after many hours of drawing pictures but now I am stuck on something I have pretty much no experience with. For this project I am supose to read values from a file and store in a sinlge linked list, then send a print function to a traverse function within the list class to print the contents of the list to screen, which I have working.

Then I am supose to insert each value from the single list into my double linked list in ascending order. My double linked list is fully functional but I can not seem to figure out how to pass my double list append funtion into my single list traverse function.

I will include my working double list insert function, my single list traverse function and my print function from main.

main.cpp print function:
1
2
3
4
5
template <typename T>
void print(T val)
{
     cout << val << endl;
}


single link list traverse function:
1
2
3
4
5
6
7
8
9
10
11
template <typename T>
void List<T>::traverse(void (*visit)(T &val))
{
	Node<T> *temp = head;
	
	while(temp != NULL)
	{
		(*visit)(temp->value);
		temp = temp->next;
                }
}


double linked list insert function:
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
template <typename T>
void DoublyList<T>::insert(T val)
{
	Node *newNode, *nodePtr, *previousNode;
	newNode = new Node;
	newNode->value = val;

    // Insertion into an Empty List.
    if(!head)
    {
       head = newNode;
       newNode->prev = NULL;
       newNode->next = NULL;
	   count++;
    }
    else //otherwise insert node into existing list
    {
                   nodePtr = head;
	   previousNode = NULL;

	   //cycle through the list
	   while( nodePtr != NULL && nodePtr->value < val )
	   {
		   previousNode = nodePtr;
		   nodePtr = nodePtr->next;
	   }

	   if (previousNode == NULL)
	   {
		   head = newNode;
		   newNode->next = nodePtr;
		   newNode->prev = NULL;
		   count++;
	   }
                  else
                  {
		   previousNode->next = newNode;
		   newNode->next = nodePtr;
		   newNode->prev = previousNode;
		   count++;
	  }
    }
}


This is how I pass the print function to my single list traverse function:
 
singleList.traverse(print);


when I try to pass my double list insert function
 
singleList.traverse(doubleList.insert);


I get this error: error C3867:
'DoublyList<T>::insert': function call missing argument list; use '&DoublyList<T>::insert' to create a pointer to member.

I have done some searching but am at a loss on how to fix this. Any suggestions would be appreciated!
Still trying to figure this out.
I answered your question over in the General C++ forum.
Did you tried &DoublyList<T>::insert as the compiler suggested? I think you need to modify traverse to accept a pointer to member function
void List<T>::traverse( void (DoublyList::*visit)(T) ) // where do you insert the values?

However I think that is better to pass a reference to a DoublyList
1
2
3
4
void List<T>::copy(DoublyList<T> &DL){
  //...
  DL.insert( temp->value );
}

Last edited on
Thank you for all the suggestions. I tried them all and had no luck. I think I am going to just make a get value function and run a foor loop in main. Im not too excited about doing this as I really want to learn how to pass another classes function to my single link list traverse but submitting a functional program for some points is better than no points.
If you pass an pointer to member function (or attribute) , you need to pass an object too (or create one in the function)
Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<class T>
class A{
public:
    void print(T v){
        std::cout<<"hello_world "<<v<<std::endl;
    }
};

template<class T>
class B{
public:
    void receive(A<T> &a, void (A<T>::*func)(T) ){ //sadly it seems that you can't make a typedef of a template
        std::cout<< "Inside class B "<<std::endl;
        (a.*func)(42);
    }
};

int main(){
  A<int> a;
  B<int> b;
  b.receive( a, &A<int>::print); //see how it is passed. You need to get an address because you ask for a pointer
}


To avoid the loop in main you can try to copy the copy function.
Or implement a copy method in List, that will receive any container that implements an insert(T) method
Thank you all! I went with a copy method but I did learn a bit more about passing functions as parameters.
Topic archived. No new replies allowed.