Why not tel me something about pointers?

L

Link

//Binary Tree
//Compiled with Microsoft Visual C++ 6.0


typedef enum {one=1,two} timesPushed;

#include <string>

#include "CTStack.h" //my simple stack

using namespace std; //basic_string<...>

template <class T>

class BinTreeNode {

public:

BinTreeNode(T dat,BinTreeNode<T> *left=0,BinTreeNode<T> *right=0)

{

data=dat;

llink=left;

rlink=right;

}

BinTreeNode():llink()),rlink(0){}

BinTreeNode *llink,*rlink;

T data;

};

template <class T>

class BinTree {

public:

BinTreeNode<T> *root;

BinTree():root(0){}

~BinTree(){destroy();}

bool creat(const T *pre_,const T *in_)

{

string pre(pre_),in(in_);

return creat(pre,in);

}

bool creat(basic_string<T> &pre,basic_string<T> &in)

{

BinTreeNode<T> *curNode,*newNode;

stack<BinTreeNode<T> *> nodes;

stack<unsigned> position;

unsigned inPos,prePos,prelen,inlen,curPos;

prelen=pre.length();

inlen=in.length();

if (prelen!=inlen) return false;

if (prelen*inlen==0) return true;

for (inPos=0;inPos<inlen;++inPos)

{

if (in[inPos]==pre[0]) break;

}

if (inPos==inlen)

return false;

root=new BinTreeNode<T>(pre[0]);

nodes.push(curNode=root);

position.push(curPos=inPos);

for (prePos=1;prePos<prelen;++prePos)

{

for (inPos=0;inPos<inlen;++inPos)

{

if (in[inPos]==pre[prePos]) break;

}

if (inPos==inlen)

return false;

newNode=new BinTreeNode<T>(pre[prePos]);

if (inPos<curPos)

nodes.gettop()->llink=newNode;

else {

while
(!position.isEmpty()&&inPos>position.gettop())

{

curNode=nodes.pop();

curPos=position.pop();

}

curNode->rlink=newNode;

}

nodes.push(newNode);

position.push(curPos=inPos);

}

return true;

}

bool postOrderTraverse(bool (*visitor)(BinTreeNode<T> *))

{

stack<BinTreeNode<T> *> pointers;

stack<timesPushed> seen;

BinTreeNode<T> *curPtr=root;

timesPushed pushed;

while (curPtr!=0||!pointers.isEmpty())

{

if (curPtr!=0) {

pointers.push(curPtr);

seen.push(one);

curPtr=curPtr->llink;

}

else {

curPtr=pointers.pop();

pushed=seen.pop();

if (pushed==one) {

pointers.push(curPtr);

seen.push(two);

curPtr=curPtr->rlink;

}

else {

if (!(*visitor)(curPtr))

return false;

curPtr=0;

}

}

}

return true;

}

private:

bool deleteHelper(BinTreeNode<T> *ptr)

{

delete ptr;

return ture;

}

};



Compiling...

TemplateFunctionPointers.cpp

F:\Algorithms\Experiments\TemplateFunctionPointers.cpp(104):

error C2664: 'postOrderTraverse' : cannot convert parameter 1 from 'bool
(class BinTreeNode *)' to 'bool (__cdecl *)(class BinTreeNode *)'

None of the functions with this name in scope match the target type

F:\Algorithms\Experiments\TemplateFunctionPointers.cpp(103) : while
compiling class-template member function 'void __thiscall
BinTree::destroy(void)'

Error executing cl.exe.
 
R

Rolf Magnus

Link said:
Compiling...

TemplateFunctionPointers.cpp

F:\Algorithms\Experiments\TemplateFunctionPointers.cpp(104):

Which line is 104? When I count them in your code, I get to an empty line.
error C2664: 'postOrderTraverse' : cannot convert parameter 1 from 'bool
(class BinTreeNode *)' to 'bool (__cdecl *)(class BinTreeNode *)'

None of the functions with this name in scope match the target
type

F:\Algorithms\Experiments\TemplateFunctionPointers.cpp(103) :
while
compiling class-template member function 'void __thiscall
BinTree::destroy(void)'

There is no destroy() function in your code.
 
L

Link

I'm sorry.
It was my fault that I just didn't wrote destroy() in the original message.
It should be:
void destroy(void)
{
postOrderTraverse(deleteHelper); ////// line 104
}
And line 104 is where a pointer to function:
bool deleteHelper(BinTreeNode<T> *)
is used.
----- Original Message -----
From: "Rolf Magnus" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Saturday, May 07, 2005 5:15 PM
Subject: Re: Why not tel me something about pointers?
 
R

Rolf Magnus

Link said:
I'm sorry.
It was my fault that I just didn't wrote destroy() in the original
message. It should be:
void destroy(void)
{
postOrderTraverse(deleteHelper); ////// line 104
}
And line 104 is where a pointer to function:
bool deleteHelper(BinTreeNode<T> *)
is used.

Ah well, deleteHelper is a member function of BinTree. You cannot call a
(non-static) member function through a normal function pointer. That is
because the member function is always called in the context of an object
and a regular function isn't. How would the compiler know which object to
call the function for? Therefore, pointers to non-static member functions
and normal function pointers are incompatible. Try making the member
function static. It doesn't need a BinTree object anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top