Function Pointers

Hi,
I'm trying to make a process function for my doubly linked list. I have a fully implemented doubly linked list designed as well as the nodes.

I wanted to put this function in DLNode class
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
    void incrementData()
    {
        data++;
    }

and this function in my DLList class
    void process(void (*f)())

    {
        for(DLNode<DATA>* temp = head; temp != NULL; temp = temp->getNext() )
        {
            temp->f();
        }
    }

I'm having scope issues, this is where i try to use it.(in main)

    DLList<int> DLL;
    DLL.pushBack(1);
    DLL.pushBack(2);
    DLL.pushBack(3);
    DLL.pushBack(4);
    cout << DLL << endl;
    DLL.process(incrementData);
    cout << DLL << endl; 


error: 'incrementData' was not declared in this scope|

Last edited on
Is incrementData part of DLList class? If yes than you have to call is using the object.
If that's a global function, its declaration should be inside a header file.
otherwise if you don't have a header file, it should be defined at the top of the .cpp file.
Its part of my node class since that's where the data member 'data' is stored. My dllist and
dlnode classes are bother h files only. Trying to avoid problems with cross platform compilation
And the code which you have written is in node class or in main? If its in main than you cannot call incrementData without class object.

You may have to post more code to make it more clear.
i figured out my problem. it was in the process implementation. should have been.

1
2
3
4
5
6
7
void process(void (*f)(DATA))
{
      for(DLNode<DATA>* temp = head; temp != NULL; temp = temp->getNext() )
     {
        f(temp->getData());
     }
}

I also should've had my incrementData function global.
Last edited on
cool..
Topic archived. No new replies allowed.