Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<iostream.h>
#include<conio.h>
#include<process.h>

struct tree_node
{
	tree_node *left;
	tree_node *right;
	int data;
} ;
class bst
{
	tree_node *root;
	public:
	bst()
	{
		root=NULL;
	}
	int isempty() 
	{
		return(root==NULL);
	}
	void insert(int item);
	bool search(tree_node * ,int); // 2 errors on this line
                                       // 1. Type name expected.
                                       // 2. Declaration missing ;   
};
bool search(tree_node* root,int data) { //error on this line(decl. syntax error)
	if(root == NULL) {
		return false;
	}
	else if(root->data == data) {
		return true;
	}
	else if(data <= root->data) {
		return Search(root->left,data);
	}
	else {
		return Search(root->right,data);
	}
}
void main()
{
	int digit;
	bst b;
        int number;
	cout<<"Enter number be searched\n";
	cin>>number;
	//If number is found, print "FOUND"
	if (b.search(root,number) == true) cout<<"Found\n";
	else cout<<"Not Found\n";
	getch();
}

the search function of this code is giving three errors
1. Type name expected.
2. Declaration missing ;
3. Declaration syntax error.
please rectify.
Posted
Updated 25-Nov-14 1:25am
v4
Comments
[no name] 11-Nov-14 1:59am    
What do you mean by rectify as soon as possible? Is this a way to ask my friend!
Varun Galar 25-Nov-14 7:26am    
@suraj sorry friend
Can u help me out to solve the errors in this code.
Sergey Alexandrovich Kryukov 25-Nov-14 10:11am    
Yes, I can now, thanks to your clarification. Please see Solution 2.
—SA
Sergey Alexandrovich Kryukov 11-Nov-14 2:00am    
"Is not working fine" is not informative. And please format your code properly using the "pre" tags (with the attribute lang="c++"). Your first 3 lines are "#include". You forgot to escape HTML angular brackets.
Please explain the problem properly, in sufficient detail.
—SA
Varun Galar 25-Nov-14 7:31am    
@sergey I have mentioned the errors in the code in comment form
please help me out.

1. Fix all the irritating compiler errors that'll occur when you try and build that source file

2. Consider the interface to the search member function. Are you sure it should have that interface? Should it be a public member? Should there be more versions of search than you've specified?

3. Implement insert

4. Actually insert some data in your main function so you can search for it

5. If you haven't realised where the problem is by then come back and someone can make some more suggestions.
 
Share this answer
 
This is not how "iostream.h" should be included. You need to use #include <iostream>. Yes, this is how std library headers are designed.

Now, there is no an error in this line:
C++
bool search(tree_node * ,int); // 2 errors on this line.

Generally, your problem is reporting the problem not with the code you were trying to compile. You should not have put this part in the CPP file with the entry-point ("main"), it belongs to the include file and just makes no sense, and then the implementation would be in a separate CPP file you feed to the compilation. If you want to have it in the same file (because other files are not using this class and structure), you should better implement all functions inside the class definition. But if you do it separately, this definition
C++
bool search(tree_node* root,int data) { /* ... */ }
would not define the class function search. To write such definition, you should write it as
C++
bool bst::search(tree_node* root,int data) { /* ... */ }

Conclusion: you need to learn few very basic things: how C/C++ separate compilation works, as well as C++ class syntax for declaration of classes and definitions of their members.

Good luck,
—SA
 
Share this answer
 

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