Hello all I'm trying to pass a function pointer to my traverse function by defining a new function called decreaseByOne that will decrease the value of the value in each node in a linked list by one. This is my first time trying to use a function pointer and its giving me some problems.
If I need to post the rest of the code in the program let me know. I know (*visit) is my function pointer and (q->entry) would be the parameter for the function it is pointing to. I'm not sure if (*vist) = decreaseByOne(); is correct or if (T *e) would be right for the decreaseByOne function. Any tips would be greatly appreciated!
> and its giving me some problems.
you need to be more specific
- compilation error
- wrong output
- crash
- computer exploded
> void Deque<T>::decreaseByOne(T *e)
¿why is that a member function? you don't seem to use the object at all
remember that a member function may be written equivalent in C as void decreaseByOne(Deque_T * constthis, T *e)
Hey ne555 thanks for the help! I'm getting a bunch of compilation errors. For the decreaseByOne function all it needs to do is have a parameter for (q->entry) and increase it by 1(the value in the node). Is (*vist) = decreaseByOne(); correct? I'm trying to have visit be the function pointer for the decreaseByOne function.
In file included from pa1.cpp:4:0:
Deque.h: In member function âvoid Deque<T>::traverse(void (*)(T&))â:
Deque.h:71:4: error: âvistâ was not declared in this scope
pa1.cpp: In function âint main()â:
pa1.cpp:126:14: error: invalid conversion from âintâ to âvoid (*)(int&)â [-fpermissive]
Deque.h:69:6: error: initializing argument 1 of âvoid Deque<T>::traverse(void (*)(T&)) [with T = int]â [-fpermissive]
In file included from pa1.cpp:4:0:
Deque.h: In member function âvoid Deque<T>::traverse(void (*)(T&)) [with T = int]â:
pa1.cpp:126:14: instantiated from here
Deque.h:71:2: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say â&Deque<int>::decreaseByOneâ [-fpermissive]
Deque.h: In member function âvoid Deque<T>::decreaseByOne(const T*) [with T = int]â:
Deque.h:71:2: instantiated from âvoid Deque<T>::traverse(void (*)(T&)) [with T = int]â
pa1.cpp:126:14: instantiated from here
Deque.h:65:4: error: request for member âentryâ in â* eâ, which is of non-class type âconst intâ
> error: `vist' was not declared in this scope
the parameter name is `visit' not `vist'
> error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.
> Say `&Deque<int>::decreaseByOne' (*vist) = decreaseByOne();
To assign a function pointer it should be pointer = &function (note that there is no dereference, the use of & and the lack of parenthesis)
However, ¿why are you assigning to your parameter? ¿what's the point of asking for it, if you are just going to throw its value?
Again, note that the error indicates that the function is a member function
> error: request for member `entry' in `* e', which is of non-class type `const int'
The implementation of `decreaseByOne()' seems to ask for a `Node*'
you are passing an `int' instead'
Change the implementation or the call.
By the way e->entry + 1; //statement has no effect
Thanks for the help ne555 I moved the decreaseByOne function into the main and made one for char and one for int then passed that into the traverse functions and it works!