What I want to do is pass a function to a function. The function that is to receive a function is a recursive function. It is to call the passed function. I have a short (one page) example of this in my book but that is it and I can't get the compiler to accept the code.
The error is stating of a mismatch between the formal and actual parameters, error converting from " void (Node &) to Vode (_cdecl *)(Node *) " The prototypes are void PrintNodeData(Node *sentNode); and int RecursiveDataPrint(Node* sentPtr,void(*sent)(Node *));. I then call with this line RecursiveDataPrint(Root,PrintNodeData); The error is with parameter 2. I don't know what the compiler is saying when it has "(_cdecl *)".
Any ideas?
Randy
shortLived 1 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
>I can't get the compiler to accept the code
So post the code. It's easier to figure out your problem when you give us more than just the bits and pieces that you think are causing the problem.
shortLived 1 Newbie Poster
Here is the class, and its .h file. There are 4 other classes that all work together. I really only need help for a small portion of this one class. The two methods of interest are int RecursiveDataPrint(Node* sentPtr,void(*sent)(Node *)); and void PrintNodeData(Node *sentNode);
The error at compile is "z:\mydocuments\Visual Studio Projects\Data Structures\BinaryTree\BinaryTree.cpp(40) : error C2664: 'BinaryTree::RecursiveDataPrint' : cannot convert parameter 2 from 'void (Node *)' to 'void (__cdecl *)(Node *)' "
Line 40 is " RecursiveDataPrint(Root,PrintNodeData);" in the PrintList method.
Let me know if more code is needed.
#ifndef MyTree_h
#define MyTree_h
#include ".\node.hpp" // data storage
class BinaryTree
{
public:
BinaryTree(void);
~BinaryTree(void);
int CreateNode(void);
void PrintList(void);
int Search(int, Node*);
int InsertNode(Node* sentNode);
int ResortTree(void);// not implemented yet
private:
Node* Root;
// For general use for the methods of this class
Node* currentPointer;
int RecursiveDataPrint(Node* sentPtr,void(*sent)(Node *));
int totalNodeCount;
void PrintNodeData(Node *sentNode);
};
#endif
.cpp file ------------
#include ".\binarytree.hpp"
#include<iostream>
//#include<string>
using namespace std;
BinaryTree::BinaryTree(void)
: Root(0)
, currentPointer(NULL)
, totalNodeCount(0)
{
}// EO Constructor
BinaryTree::~BinaryTree(void)
{
}
// *************************************************
// CreateNode Method
// *************************************************
int BinaryTree::CreateNode()
{
int iResult = ERROR;
Node* objAddedNode = new Node(); // a temporary node is created
iResult = InsertNode(objAddedNode);
delete objAddedNode;
return iResult;
}// EOF CreateNode
// *************************************************
// PrintList Method
// *************************************************
void BinaryTree::PrintList(void)
{
if (Root == 0)
cout << " The tree is Empty " << endl;
else{
RecursiveDataPrint(Root,PrintNodeData);
//Root->PrintAgentData();
//RecursiveDataPrint(Root->GetRightNodePtr());
}
}// EOF PrintList
// *************************************************
// RecursiveDataPrint
// *************************************************
int BinaryTree::RecursiveDataPrint(Node* sentPtr, void(*sent)(Node &))
{
//int iResult = ERROR;
if (sentPtr->GetLeftNodePtr() == 0 && sentPtr->GetRightNodePtr() ==0)
(*sent)(sentPtr);
//sentPtr->PrintAgentData();
else{
if(sentPtr->GetLeftNodePtr() != 0)
RecursiveDataPrint(sentPtr->GetLeftNodePtr(),sent);
(*sent)(sentPtr);
//sentPtr->PrintAgentData();
if (sentPtr->GetRightNodePtr() != 0)
RecursiveDataPrint(sentPtr->GetRightNodePtr(),sent);
}
return 0;
}// EOF RecursiveDataPrint
// *************************************************
// Search Method
// *************************************************
int BinaryTree::Search(int searchData, Node* sentNode)
{
int iResult = ERROR;
if(Root == 0)
iResult = EMPTY;
else
{
bool bEndSearch = false;
currentPointer = Root;
while(currentPointer != 0 && !bEndSearch){
if(currentPointer->GetAgentNumber() == searchData){
// found the data
bEndSearch = true;
sentNode->SetAgentName(currentPointer->GetAgentName());
sentNode->SetAgentNumber(currentPointer->GetAgentNumber());
}
if(currentPointer->GetAgentNumber() < searchData)
currentPointer = currentPointer->GetRightNodePtr();
else
currentPointer = currentPointer->GetLeftNodePtr();
}
if (bEndSearch)
iResult = SUCCESS;
}
return iResult;
}// EOF Search
// *************************************************
// InsertNode Method
// *************************************************
int BinaryTree::InsertNode(Node* sentNode)
{
// Traverse the tree looking for the position.
//If a node exists with same data return error.
// If not insert node and set pointers.
// increment total count// to be added.
int iResult = ERROR;
Node* objNewNode = new Node(1);
objNewNode->SetAgentName(sentNode->GetAgentName());
objNewNode->SetAgentNumber(sentNode->GetAgentNumber());
if (Root == 0){
Root = objNewNode;
iResult = SUCCESS;
}
else{
Node* trailerPointer;
bool bDirection = true; // I have true = left, false = right..
currentPointer = Root;
while( currentPointer != 0 && iResult != OVERFLOW )
{// continue search
trailerPointer = currentPointer; // trailer pointer to the parent
if(sentNode->GetAgentNumber() < currentPointer->GetAgentNumber())
{// Go left
bDirection = true;
currentPointer = currentPointer->GetLeftNodePtr();
}
else if (sentNode->GetAgentNumber() > currentPointer->GetAgentNumber())
{// go right
bDirection = false;
currentPointer = currentPointer->GetRightNodePtr();
}
else
iResult = OVERFLOW ;
}
if (iResult != OVERFLOW){
if(bDirection)
trailerPointer->SetLeftNodePtr(objNewNode);
else
trailerPointer->SetRightNodePtr(objNewNode);
iResult = SUCCESS;
totalNodeCount++;// increment node count // to be added
}
else
iResult = ERROR; // this indicates a matching data value found.`
}
return iResult;
}// EOF InsertNode
// ResortTree Method
int BinaryTree::ResortTree(void)
{
//move data from tree to array.
// clear all nodes from tree.
// Reinsert nodes via binary retreival from array.
Node storageArray[totalNodeCount];
return 0;
}
// PrintNodeData method
void BinaryTree::PrintNodeData(Node *sentNode)
{
sentPtr->PrintAgentData();
}
<< moderator edit: added code tags: [code][/code] >>
Narue 5,707 Bad Cop Team Colleague
PrintNodeData is a member function, and you treat pointers to member functions differently than pointers to functions.
shortLived 1 Newbie Poster
Thanks for the information. I will study it for a few days.
shortLived 1 Newbie Poster
Ok I looked over the information form the link you suggested. I think it is good but not quite helpful to me. The first sample simply shows using a pointer, one pointer happens to point to a function. I don't see how this can help me. The other code sample just confused me and also looks like it goes the wrong direction. What I would like to know is it possible for a method of a class to receive a pointer to another method and have that pointer be reassigned. I don't just want to use an alias I want to change how the receiving method acts. I could include a int value and pass a code that will determine how the method will act. But I just wanted to know if I could have a parameter of a method take a pointer to a method and have this pointer modified when necessary.
#include <iostream>
struct s {
int i;
void f() { std::cout<<"i is "<< i <<'\n'; }
};
int main()
{
s a, b;
int s::*pi = &s::i;
void (s::*pf)() = &s::f;
a.*pi = 10;
b.*pi = 20;
(a.*pf)(); ////// this is the same as a.f(); is it not?
(b.*pf)();
}
Narue 5,707 Bad Cop Team Colleague
>What I would like to know is it possible for a method of a class to receive
>a pointer to another method and have that pointer be reassigned.
Yes.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.