Calling an object's function in C++

clamum

Lifer
Feb 13, 2003
26,252
403
126
I have a class called TreeNode with a function called getLeft() and a function called getRight(), which returns the left sibling and right sibling of the binary tree node, respectively.

I also have a class called Tree, and a function that returns if a node is a leaf (no children). So I want to call a node's getLeft() and getRight() functions. the isLeaf() method has a pointer to a TreeNode as a parameter (named "v").

The question is, how do I call v's getLeft() and getRight() methods? So far I have v->getLeft() but I'm getting an error that's so useful it's funny ("syntax error at end of input").

I'm a n00b to C++ (only had a few Java classes last year and this year) so thanks for any help. The code for the isLeaf method is below:
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Did you make the TreeNode functions public: ? Are you including the .h file(s) that define Tree and TreeNode ?

You might paste the class def for TreeNode into your code snippet above
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
I had the TreeNode functions private, but changed them to public and I am still getting the same error.
The .h files for the class definitions of Tree and TreeNode are included in the Tree.C file.

It's probably some pointer error... I don't really understand them that well at this point.

Here's the class definition for TreeNode (minus some big block comments):
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
Well you have a problem here. First off, you return a TreeNode from your get functions instead of a TreeNode*. When you do this, the returned TreeNode is a copy. However, the pointers in both the original and the copy are the same. This means you are going to run into all sorts of problems if you change the child or the parent in the returned TreeNode.

Instead, you should return a TreeNode pointer. It would make the code look like this:
 

imported_obsidian

Senior member
May 4, 2004
438
0
0
Also, as you can see, I put in a default constructor. I would suggest that as well. I believe that would also fix your initial problem if you decide not to return pointers. Also, here is some compilable sample code to test:
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Ok I added the TreeNode pointer return type but now I'm getting this error:

TreeNode.h:17: error: syntax error before 'namespace'

Line 17 is "using namespace std;" ... I don't see what's wrong with that though.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Try to put the namespace declaration before the #include
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Why does your header include <iostream> anyways? It doesn't need it. And you should avoid "using" statements in headers. Headers shouldn't pollute the global namespace.

And I'm not sure how putting the "using" statement before the include would help -- in fact I would guess that could be the current problem.
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
This isn't working very well... Now I'm getting these errors:

In file included from tree.h:16,
from tree.C:14:
TreeNode.h: In member function `TreeNode* TreeNode::getLeft()':
TreeNode.h:53: error: cannot convert `TreeNode' to `TreeNode*' in return
TreeNode.h: In member function `TreeNode* TreeNode::getRight()':
TreeNode.h:56: error: cannot convert `TreeNode' to `TreeNode*' in return
TreeNode.h: In member function `TreeNode* TreeNode::getParent()':
TreeNode.h:59: error: cannot convert `TreeNode' to `TreeNode*' in return
tree.C: At global scope:
tree.C:17: error: syntax error before `namespace'
tree.C:59: error: syntax error at end of input

The code for each file is below. I left out a lot of unimportant stuff.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
the return type is pointer so you should not dereference it. so change TreeNode * getLeft() { return * left; } to TreeNode * getLeft() { return left; } and so on for the other ones

as for the error at 17, maybe it's something near the end of tree.h
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Originally posted by: dighn
the return type is pointer so you should not dereference it. so change TreeNode * getLeft() { return * left; } to TreeNode * getLeft() { return left; } and so on for the other ones

as for the error at 17, maybe it's something near the end of tree.h

Ah, that's right. Can't believe I didn't see that before.

As for the error in tree.h, I forgot a damn end brace.

I just compiled and got a bunch more errors but hopefully I can fix them. Thanks for the help!

EDIT: Fixed them. Thanks for the help everyone.