Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
for the line defining print function
error : A function definition is not allowed before '{' token

for the last line of my code
error : expected '}' at the end of the input

What I have tried:

C++
#include <iostream>

using namespace std;

struct node
{
	int data;
	node*p_right;
	node*p_left;
};

node* insert_(node*p_tree,int key)
{
	if(p_tree==NULL)
	{
		node*p_new_tree=new node;
		p_new_tree->p_left=NULL;
		p_new_tree->p_right=NULL;
		p_tree=p_new_tree;
		return p_tree;

	}
	else if(key<p_tree->data)
	{
		p_tree->p_left=insert_(p_tree->p_left,key);
		return p_tree;
	}
	else if(key>p_tree->data)
	{
		p_tree->p_right=insert_(p_tree->p_right,key);
		return p_tree;
	}

	int print(node*p_tree)
	{
		if (p_tree==NULL)
		{
			return NULL;
		}
		else
		{
			cout <<  p_tree->data;
			print(p_tree->p_left);
			print(p_tree->p_right);

		}

	}

}

int main()
{ 
	node*p_tree=NULL;

	insert_(p_tree,4);
	insert_(p_tree,5);
	insert_(p_tree,3);
	insert_(p_tree,6);
	insert_(p_tree,2);
	insert_(p_tree,1);
	insert_(p_tree,7);
	insert_(p_tree,8);

	print(p_tree);
}
Posted
Updated 13-Jun-16 21:32pm
v3
Comments
PIEBALDconsult 14-Jun-16 0:36am    
Consistent indenting makes the error more obvious.
print appears to be defined within insert_node -- relocate the } at the end of insert_node
Peter_in_2780 14-Jun-16 0:42am    
:thumbsup: just beat me to it...
OriginalGriff 14-Jun-16 1:09am    
Suggest you post this as the solution!

1 solution

In C and C++ language, you can't define a function inside another one.
You must move print outside of insert_

Indentation is here to help you to read your code. proper use of indentation prevent this kind of problem.
I recommend the usage of a programmer's editor like NotePad++.
Notepad++ Home[^]
 
Share this answer
 
v2
Comments
CPallini 14-Jun-16 3:48am    
5.
Patrice T 14-Jun-16 4:05am    
Thank you
Member 12566145 14-Jun-16 4:50am    
thank you

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