Using function pointers

Hey guys,

I'm currently writing code for a Binary Tree and was given the functions for inorder and preorder traversals that take a function pointer as an argument. Our lab TA didn't really discuss how to deal with these and I'm having a bit of trouble deciphering what I've found on google.

Here's what I've got for the declarations(given to us):
1
2
   virtual void preorderTraverse(FunctionType visit);
   virtual void inorderTraverse(FunctionType visit);


My visit function:
1
2
3
void BinaryTree::visit(TreeItemType& anItem) {
	cout << anItem;
}


The typedefs:
1
2
typedef char TreeItemType;
typedef void (*FunctionType)(TreeItemType& anItem);


My whack at implementing:
1
2
3
4
5
6
	FunctionType v = &BinaryTree::visit;
	cout << "Result of inorder traversal: ";
	inorderTraverse(v);
	cout << "Result of preorder traversal: ";
	preorderTraverse(v);
	cout << endl << endl;


Error I'm getting:
BinaryTree.cpp:29:32: error: cannot convert ‘void (BinaryTree::*)(TreeItemType&) {aka void (BinaryTree::*)(char&)}’ to ‘FunctionType {aka void (*)(char&)}’ in initialization

We are unable to change the preorder/inorder code, the typedefs, or those declarations. I'm just asking if anyone can lead me in the right direction or tell me how I would go about getting the function calls correct.

Thank you very much for your help,
Chris

Edit: That error is talking about the "FunctionType = &BinaryTree::visit;".
Last edited on
Try just:
1
2
3
4
5
6
7
8
//first create a BinaryTree. (Let's assume the next line does that for you.)
BinaryTree tree;

cout << "Result of inorder traversal: ";
tree.inorderTraverse( &BinaryTree::visit );

cout << "Result of preorder traversal: ";
tree.preorderTraverse( &BinaryTree::visit );

I think both inorderTraverse and preorderTraverse want to do something with each element, but don't specify what. It looks like BinaryTree::visit is a static method (i.e. class function) which prints a node. All you need to do is say to the traversal method "hey, I want you to BinaryTree::visit each item" it will do the work fo you (in its correct order.)

To see this better:
1
2
3
4
5
6
7
8
9
10
//define a new function (like the example one below)
void guessNode(TreeItemType &item) {
    cout << "guess: ";
    TreeItemType i;
    cin >> i;
    cout << (i == item ? "yep" : "nope") << endl;
}

//then call it on each element in your BinaryTree with one of the traversal methods
tree.preorderTraverse( guessNode ); //The & should be optional for function names 
I tried what you said, but I am essentially getting the same error.
I am getting:
1
2
3
4
BinaryTree.cpp:398:37: error: no matching function for call to ‘BinaryTree::preorderTraverse(void (BinaryTree::*)(TreeItemType&))’
BinaryTree.cpp:398:37: note: candidate is:
BinaryTree.cpp:238:6: note: virtual void BinaryTree::preorderTraverse(FunctionType)
BinaryTree.cpp:238:6: note:   no known conversion for argument 1 from ‘void (BinaryTree::*)(TreeItemType&) {aka void (BinaryTree::*)(char&)}’ to ‘FunctionType {aka void (*)(char&)}’


If you look in the last line of the error, it says there is no known conversion from void (BinaryTree::*)(char&) to void (*)(char&).

Edit:

Just so you can see the code for the inorder and preorder traversals, I have included it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void BinaryTree::preorder(TreeNode *treePtr,
                          FunctionType visit)
{
   if (treePtr != NULL)
   {  visit(treePtr->item);
      preorder(treePtr->leftChildPtr, visit);
      preorder(treePtr->rightChildPtr, visit);
   }  // end if
}  // end preorder

void BinaryTree::inorder(TreeNode *treePtr,
                         FunctionType visit)
{
   if (treePtr != NULL)
   {  inorder(treePtr->leftChildPtr, visit);
      visit(treePtr->item);
      inorder(treePtr->rightChildPtr, visit);
   }  // end if
}  // end inorder 


And the function calls I've been using:
1
2
3
4
5
6
7
8
9
void BinaryTree::preorderTraverse(FunctionType visit)
{
   preorder(root, visit);
}  // end preorderTraverse

void BinaryTree::inorderTraverse(FunctionType visit)
{
   inorder(root, visit);
}  // end inorderTraverse 
Last edited on
Topic archived. No new replies allowed.