Aug 14, 2011 at 6:51am UTC
Error Occured: no matching function for call to `search(int)'
as I gave the def of search(Object data)
using template so why it is not recognizing.
I implemented insert_at_tail(Object data) using same template approach but it is not compiling this time :(
#include <iostream>
using namespace std;
template <class Object>
class Linked_List
{
private:
struct Node
{
Object info;
Node *next;
Node() : info(0), next(NULL)
{ }
};
Node *head;
Node *tail;
int no_of_rec;
public:
Linked_List(): head(NULL), tail(NULL), no_of_rec(0)
{ }
Node* create_node()
{
no_of_rec++;
return new Node;
}
void free_node(Node *p)
{
p->next = NULL;
no_of_rec--;
delete p;
}
void insert_at_tail(Object data)
{
Node *p;
p = create_node();
p->info = data;
if(tail == NULL)
tail = p;
else
tail->next = p;
p->next = NULL;
if(head == NULL)
head = p;
tail = p;
}
void destroy_list()
{
Node *temp = head;
while(head != NULL)
{
temp = head;
head = head->next;
free_node(temp);
}
}
void display_list()
{
cout << endl << "Contents of the List are: " << endl;
Node *current;
current = head;
while(current != NULL)
{
cout << current->info << endl;
current = current->next;
}
}
int size()
{ return no_of_rec; }
~Linked_List()
{ destroy_list(); }
bool search(Object data)
{
Node* p = head;
while(p != NULL)
{
if(p->info == data)
break;
p = p->next;
}
if(p == NULL)
return false;
else
return true;
}
};
int main()
{
Linked_List<int> L1;
L1.insert_at_tail(1);
L1.insert_at_tail(2);
L1.insert_at_tail(3);
L1.insert_at_tail(4);
L1.display_list();
bool found = false;
found = search(4);
if(found == true)
cout << "Found" << endl;
else
cout << "Not Found" << endl;
return 0;
}
Aug 14, 2011 at 6:52am UTC
Put some code tags around that and paste the entire error (with line numbers) please.
Aug 14, 2011 at 6:58am UTC
there is only one error in the compilation and the error line is function call:
found = serach(4);
I didn't call the search func in any other place
Aug 14, 2011 at 6:58am UTC
Your call to search
isn't tied to an instance of Linked_List
. Try L1.search(4);
Aug 14, 2011 at 7:25am UTC
stupid mistake:(
Sorry people but thanks