Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I need some help with this, I'm trying to call a function from my header file to my main, this function return a structure.

The problem here is that I don't get how to call a function that returns a structure from it, I have tried some things but nothing has worked.

Output

C#
error: request for member 'Insert' in 'root', which is of pointer type 'TREE::tree*' (maybe you meant to use '->' ?)
   root = root.Insert(root, 9);


C#
error: request for member 'Print' in 'root', which is of pointer type 'TREE::tree*' (maybe you meant to use '->' ?)
  root.Print(root);



The header file

C++
#ifndef _LibArb_H_
#define _LibArb_H_

struct Node{
	
	int val;
	Node *left, *right;
	
};

using namespace std;
namespace TREE
{

class tree
{

	private:
	
		Node *node;
		
	public:
	
		tree();
		~tree();
		
Node* Insert(Node *node, int val) // Returns a struct
{
	if(node == NULL) // If no nodes then create a new one
	{
		node = new Node;
		node->val = val;
		node->right = node->left = NULL;
	}	
	
	else if(node->val > val)
		node->left = Insert(node->left, val); // Insert value on the left node
	else
		node->right = Insert(node->right, val); // Insert value on the right node
	
	return node; // Return the value of node
}

void Print(Node *node)
{
	
	if (node)
	{
		cout << "Val: " << node->val << endl;
		cout << "Left: " << node->left << endl;
		cout << "Right: " << node->right << endl;
		Print(node->left);
		Print(node->right);
	}
}
};
}

# endif // _LibArb_H_


C++
#include <iostream>
#include "LibArb.h"

using namespace std;
using namespace TREE; // From the header file

int main()
{

	tree *root = NULL; // Create new root from the class tree of the header file
	
		root = root.Insert(root, 9); // New node with the number 9 on it
	
	root.Print(root); // Print all the nodes from the tree

	
	return 0;
}


What I have tried:

root = root->Insert(root, 9); Fail
Insert(root, 9); Fail
Posted
Updated 27-Jul-16 21:07pm

1 solution

C++
tree *root = NULL; // Create new root from the class tree of the header file

root = root.Insert(root, 9); // New node with the number 9 on it

The first statement creates a pointer and sets it to NULL, so it does not point to anything. You then try and call the Insert method on a NULL pointer which will always fail. You need to initialise root and then you can use it, but by -> reference, not dot; thus:
C++
tree *root = new tree(); // Create new tree pointer
// and as root is a pointer we need to use the -> reference type
root = root->Insert(root, 9); // New node with the number 9 on it

However, as your code stands, this will now overwrite the root pointer so it will be lost. I suspect the Insert method needs some work, or the node variable in the tree class should be made public.
C++
tree *root = new tree(); // Create new tree pointer
// and as root is a pointer we need to use the -> reference type
root->node = root->Insert(root, 9); // New node with the number 9 on it



You should also remove the implementation of your classes from the header file and leave just the declarations.
 
Share this answer
 
v2
Comments
CPallini 28-Jul-16 3:14am    
5.
Bit87 28-Jul-16 22:16pm    
Thanks for the explanation and for your time, at the end I had to do some changes but now it's working.

root->node = root->Insert(root->node, 9); // This is the final result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900