473,657 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,BinTree Node<T> *right=0)

{

data=dat;

llink=left;

rlink=right;

}

BinTreeNode():l link()),rlink(0 ){}

BinTreeNode *llink,*rlink;

T data;

};

template <class T>

class BinTree {

public:

BinTreeNode<T> *root;

BinTree():root( 0){}

~BinTree(){dest roy();}

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

{

string pre(pre_),in(in _);

return creat(pre,in);

}

bool creat(basic_str ing<T> &pre,basic_stri ng<T> &in)

{

BinTreeNode<T> *curNode,*newNo de;

stack<BinTreeNo de<T> *> nodes;

stack<unsigned> position;

unsigned inPos,prePos,pr elen,inlen,curP os;

prelen=pre.leng th();

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(curN ode=root);

position.push(c urPos=inPos);

for (prePos=1;prePo s<prelen;++preP os)

{

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=newNod e;

else {

while
(!position.isEm pty()&&inPos>po sition.gettop() )

{

curNode=nodes.p op();

curPos=position .pop();

}

curNode->rlink=newNod e;

}

nodes.push(newN ode);

position.push(c urPos=inPos);

}

return true;

}

bool postOrderTraver se(bool (*visitor)(BinT reeNode<T> *))

{

stack<BinTreeNo de<T> *> pointers;

stack<timesPush ed> seen;

BinTreeNode<T> *curPtr=root;

timesPushed pushed;

while (curPtr!=0||!po inters.isEmpty( ))

{

if (curPtr!=0) {

pointers.push(c urPtr);

seen.push(one);

curPtr=curPtr->llink;

}

else {

curPtr=pointers .pop();

pushed=seen.pop ();

if (pushed==one) {

pointers.push(c urPtr);

seen.push(two);

curPtr=curPtr->rlink;

}

else {

if (!(*visitor)(cu rPtr))

return false;

curPtr=0;

}

}

}

return true;

}

private:

bool deleteHelper(Bi nTreeNode<T> *ptr)

{

delete ptr;

return ture;

}

};

Compiling...

TemplateFunctio nPointers.cpp

F:\Algorithms\E xperiments\Temp lateFunctionPoi nters.cpp(104):

error C2664: 'postOrderTrave rse' : 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\E xperiments\Temp lateFunctionPoi nters.cpp(103) : while
compiling class-template member function 'void __thiscall
BinTree::destro y(void)'

Error executing cl.exe.

Jul 23 '05 #1
3 1729
Link wrote:
Compiling...

TemplateFunctio nPointers.cpp

F:\Algorithms\E xperiments\Temp lateFunctionPoi nters.cpp(104):
Which line is 104? When I count them in your code, I get to an empty line.
error C2664: 'postOrderTrave rse' : 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\E xperiments\Temp lateFunctionPoi nters.cpp(103) :
while
compiling class-template member function 'void __thiscall
BinTree::destro y(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)
{
postOrderTraver se(deleteHelper ); ////// line 104
}
And line 104 is where a pointer to function:
bool deleteHelper(Bi nTreeNode<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...

TemplateFunctio nPointers.cpp

F:\Algorithms\E xperiments\Temp lateFunctionPoi nters.cpp(104):


Which line is 104? When I count them in your code, I get to an empty line.
error C2664: 'postOrderTrave rse' : 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\E xperiments\Temp lateFunctionPoi nters.cpp(103) :
while
compiling class-template member function 'void __thiscall
BinTree::destro y(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)
{
postOrderTraver se(deleteHelper ); ////// line 104
}
And line 104 is where a pointer to function:
bool deleteHelper(Bi nTreeNode<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
380
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
5853
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
60
3314
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 want this: " Hello World" to become this: "Hello World". At first glance, my function seems to work, but returns some strange values... Here's my code (please pardon the mess):
3
3441
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, or one-past the end of the object as long as it isn't dereferenced. I use "object" in the standard 'C' sense. Is there some special dispensation given to comparing two pointers
5
1725
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
3317
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 compiler make out because //of %d that the pointer is supposed to refer to an integer ?? or is explicit type casting required ??
7
1647
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) Would this work? If so, then how to you load the data into data? Malloc?
59
5101
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
13019
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.