473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Why not tel me something about pointers?

//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.

Jul 23 '05 #1
3 1712
Link wrote:
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.

Jul 23 '05 #2
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" <ra******@t-online.de>
Newsgroups: comp.lang.c++
Sent: Saturday, May 07, 2005 5:15 PM
Subject: Re: Why not tel me something about pointers?

Link wrote:
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.

Jul 23 '05 #3
Link wrote:
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.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Link | last post by:
//Compiled with Microsoft Visual C++ 6.0 typedef enum {one=1,two} timesPushed; #include <string> #include "CTStack.h" //my simple stack
19
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
60
by: Dominique Léger | last post by:
Hello guys, I'm kinda new to C, and I'm having a hard time with strings. What I'm trying to do is a simple function that trims spaces & tabs at the beginning of a given string. For example, I...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
5
by: grocery_stocker | last post by:
Why would someone go through the trouble of constructing an error handling function using variable-length argument lists? Why not just use something like printf()?
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
7
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data)...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.