• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Calling an object's function in C++

clamum

Lifer
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:
 
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
 
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):
 
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:
 
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:
 
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.
 
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.
 
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.
 
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
 
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.
 
Back
Top