In: Computer Science
this program shows an error that
Error (active) E0308 more than one instance of overloaded function "find" matches the argument list:
can you fix that, please? debug the program output should be like that
The list is...
51, 45, 2, 13, 12, 4, 3, 5
Enter the element to be searched for
45
is present in the list.
//The program
sample.cpp
#include <iostream>
#include "node1.cpp"
using namespace main_savitch_5;
template<class iterator, class T>
iterator find(iterator start, iterator end, const T&
target);
template<class iterator, class T>
void traverseList(iterator start, iterator end);
template<class iterator, class T>
T get_data(iterator current_ptr);
int main()
{
node<int>* head = NULL;
list_head_insert(head, 5);
list_head_insert(head, 3);
list_head_insert(head, 4);
list_head_insert(head, 12);
list_head_insert(head, 13);
list_head_insert(head, 2);
list_head_insert(head, 45);
list_head_insert(head, 51);
cout << "The list is..." << endl;
traverseList<node<int>*, int > (head,
NULL);
int x;
cout << "Enter the element to be searched
for\n";
cin >> x;
node<int>* ptr = find<node<int>*,
int>(head, NULL, x);
if (ptr)
cout << x
<< "is present in the list\n";
else
cout << x
<< "is not present in the list\n";
return 0;
}
template<class iterator, class T>
iterator f(iterator start, iterator end, const T& target)
{
iterator temp = start;
while (temp != end)
{
if (get_data<iterator,
T>(temp) == target)
return
temp;
temp = get_next<iterator,
T>(temp);
}
return end;
}
template<class iterator, class T>
void traverseList(iterator start, iterator end)
{
iterator temp = start;
while (temp != end)
{
cout << get_data <
iterator, T(temp) << " ";
temp = get_next<iterator,
T>(temp);
}
cout << endl;
}
template<class iterator, class T>
T get_data(iterator current_ptr)
{
return current_ptr->data();
}
template<class iterator, class T>
iterator get_next(iterator current_ptr)
{
return current_ptr->link();
}
node1.cpp
#include "node1.h"
using namespace std;
namespace main_savitch_5
{
template<class item>
size_t list_length(const node<item>*
head_ptr)
{
const node<item>*
cursor;
size_t answer;
answer = 0;
for (cursor = head_ptr; cursor !=
NULL; cursor = cursor->link())
++answer;
return answer;
}
template<class item>
void list_head_insert(node<item>*& head_ptr,
const node<int>::value_type& entry)
{
head_ptr = new
node<item>(entry, head_ptr);
}
template<class item>
void list_insert(node<item>* previous_ptr, const
node<int>::value_type& entry)
{
node<item>* insert_ptr;
insert_ptr = new
node<item>(entry, previous_ptr());
previous_ptr->set_link(insert_ptr);
}
template<class item>
node<item>* list_search(node<item>*
head_ptr, const node<int>::value_type& target)
{
node<item>* cursor;
for (cursor = head_ptr; cursor !=
NULL; cursor = cursor->link())
return
cursor;
return NULL;
}
template<class item>
const node<item>* list_search(const
node<item>* head_ptr, const node<int>::value_type&
target)
{
const node<item>*
cursor;
for (cursor = head_ptr; cursor !=
NULL; cursor = cursor->linl())
if (target ==
cursor->data())
return cursor;
return NULL;
}
template<class item>
node<int>* list_locate(node<int>*
head_ptr, size_t position)
{
node<int>* cursor; (0 <
position);
cursor = head_ptr;
for (i = 1; (i && cursor !=
NULL);
i++)
cursor =
cursor->link();
return cursor;
}
template<class item>
const node<int>* list_locate(const
node<int>* head_ptr, size_t position)
{
const node<int>*
cursor;
size_t i;
assert(0 < position);
cursor = head_ptr;
for (i = 1; (i < position)
&& (cursor != NULL); i++)
cursor =
cursor->link();
return cursor;
}
template<class item>
void list_head_remove(node<int>* head_ptr)
{
node<int>* remove_ptr;
remove_ptr = head_ptr;
head_ptr =
head_ptr->link();
delete remove_ptr;
}
template<class item>
void list_remove(node<int>* previous_ptr)
{
node<int>* remove_ptr;
remove_ptr =
previous_ptr->link());
delete remove_ptr;
}
template<class item>
void list_clear(node<int>*& head_ptr)
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
template<class item>
void list_copy(const node<int>* source_ptr,
node<int>*& head_ptr, node<int>** tail_ptr)
{
head_ptr = NULL;
tail_ptr = NULL;
if (source_ptr == NULL)
return;
list_head_insert(head_ptr,
source_ptr->data());
tail_ptr = head_ptr;
source_ptr =
source_ptr->link();
while (source_ptr != NULL)
{
list_insert(tail_ptr, source_ptr->data());
tail_ptr =
tail_ptr->link();
source_ptr =
source_ptr->link();
}
}
}
node1.h
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include <string>
namespace main_savitch_5
{
template<class item>
class node
{
public:
typedef item value_type;
node(const value_type&
init_data = value_type(), node* init_link = NULL)
{
data_field =
init_data;
link_field =
new_data;
}
void set_data(const value_type&
new_data)
{
data_field =
new_data;
}
void set_link(node<item>*
new_link)
{
link_field =
new_link;
}
value_type data()const
{
return
data_field;
}
const node<item>*
link()const
{
return
link_field;
}
node<item>* link()
{
return
link_field;
}
private:
value_type data_field;
node<item>* link_field;
};
template<class item>
std::size_t list_length(const node<item>*
head_ptr);
template<class item>
void list_head_insert(node<item>*& head_ptr,
const node<int>::value_type& entry);
template<class item>
void list_insert(node<item>* previous_ptr, const
node<int>::value_type& entry);
template<class item>
node<item>* list_search(node<item>*
head_ptr, const node<int>::value_type& target);
template<class item>
const node<item>* list_search(const
node<item>* head_ptr, const node<int>::value_type&
target);
template<class item>
node<item>* list_locate(node<item>*
head_ptr, std::size_t position);
template<class item>
const node<item>* list_locate(const
node<item>* head_ptr, std::size_t position);
template<class item>
void list_head_remove(node<item>*&
head_ptr);
template<class item>
void list_remove(node<item>* previous_ptr);
template<class item>
void list_copy(const node<item>* source_ptr,
node<item>*& tail_ptr);
}
#endif
// node1.h
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include <string>
namespace main_savitch_5
{
template<class item>
class node
{
public:
typedef item value_type;
node(const value_type& init_data = value_type(), node* init_link = NULL)
{
data_field = init_data;
link_field = init_link;
}
void set_data(const value_type& new_data)
{
data_field = new_data;
}
void set_link(node<item>* new_link)
{
link_field = new_link;
}
value_type data()const
{
return data_field;
}
const node<item>* link()const
{
return link_field;
}
node<item>* link()
{
return link_field;
}
private:
value_type data_field;
node<item>* link_field;
};
template<class item>
std::size_t list_length(const node<item>* head_ptr);
template<class item>
void list_head_insert(node<item>*& head_ptr, const node<int>::value_type& entry);
template<class item>
void list_insert(node<item>* previous_ptr, const node<int>::value_type& entry);
template<class item>
node<item>* list_search(node<item>* head_ptr, const node<int>::value_type& target);
template<class item>
const node<item>* list_search(const node<item>* head_ptr, const node<int>::value_type& target);
template<class item>
node<item>* list_locate(node<item>* head_ptr, std::size_t position);
template<class item>
const node<item>* list_locate(const node<item>* head_ptr, std::size_t position);
template<class item>
void list_head_remove(node<item>*& head_ptr);
template<class item>
void list_remove(node<item>* previous_ptr);
template<class item>
void list_copy(const node<item>* source_ptr, node<item>*& tail_ptr);
}
#endif
//end of node1.h
//node1.cpp
#include "node1.h"
#include <cassert> // for assert
using namespace std;
namespace main_savitch_5
{
template<class item>
size_t list_length(const node<item>* head_ptr)
{
const node<item>* cursor;
size_t answer;
answer = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
++answer;
return answer;
}
template<class item>
void list_head_insert(node<item>*& head_ptr, const node<int>::value_type& entry)
{
head_ptr = new node<item>(entry, head_ptr);
}
template<class item>
void list_insert(node<item>* previous_ptr, const node<int>::value_type& entry)
{
node<item>* insert_ptr;
insert_ptr = new node<item>(entry, previous_ptr());
previous_ptr->set_link(insert_ptr);
}
template<class item>
node<item>* list_search(node<item>* head_ptr, const node<int>::value_type& target)
{
node<item>* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
return cursor;
return NULL;
}
template<class item>
const node<item>* list_search(const node<item>* head_ptr, const node<int>::value_type& target)
{
const node<item>* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->linl())
if (target == cursor->data())
return cursor;
return NULL;
}
template<class item>
node<int>* list_locate(node<int>* head_ptr, size_t position)
{
size_t i;
node<int>* cursor; (0 < position);
cursor = head_ptr;
for (i = 1; (i < position && cursor != NULL);
i++)
cursor = cursor->link();
return cursor;
}
template<class item>
const node<int>* list_locate(const node<int>* head_ptr, size_t position)
{
const node<int>* cursor;
size_t i;
assert(0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link();
return cursor;
}
template<class item>
void list_head_remove(node<int>* head_ptr)
{
node<int>* remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link();
delete remove_ptr;
}
template<class item>
void list_remove(node<int>* previous_ptr)
{
node<int>* remove_ptr;
remove_ptr = previous_ptr->link();
delete remove_ptr;
}
template<class item>
void list_clear(node<int>*& head_ptr)
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
template<class item>
void list_copy(const node<int>* source_ptr, node<int>*& head_ptr, node<int>*& tail_ptr) // change the node<int> **tail_ptr to node<int>*& tail_ptr
{
head_ptr = NULL;
tail_ptr = NULL;
if (source_ptr == NULL)
return;
list_head_insert(head_ptr, source_ptr->data());
tail_ptr = head_ptr;
source_ptr = source_ptr->link();
while (source_ptr != NULL)
{
list_insert(tail_ptr, source_ptr->data());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
}
}
//end of node1.cpp
// sample.cpp
#include <iostream>
#include "node1.cpp"
using namespace main_savitch_5;
template<class iterator, class T>
iterator find(iterator start, iterator end, const T& target);
template<class iterator, class T>
void traverseList(iterator start, iterator end);
template<class iterator, class T>
T get_data(iterator current_ptr);
// include the declaration of get_next
template<class iterator, class T>
iterator get_next(iterator current_ptr);
int main()
{
node<int>* head = NULL;
list_head_insert(head, 5);
list_head_insert(head, 3);
list_head_insert(head, 4);
list_head_insert(head, 12);
list_head_insert(head, 13);
list_head_insert(head, 2);
list_head_insert(head, 45);
list_head_insert(head, 51);
cout << "The list is..." << endl;
traverseList<node<int>*, int > (head, NULL);
int x;
cout << "Enter the element to be searched for\n";
cin >> x;
node<int>* ptr = find< node<int>*, int >(head, NULL, x);
if (ptr)
cout << x << " is present in the list\n";
else
cout << x << " is not present in the list\n";
return 0;
}
template<class iterator, class T>
iterator find(iterator start, iterator end, const T& target) // method name should be find and not f
{
iterator temp = start;
while (temp != end)
{
if (get_data<iterator, T>(temp) == target)
return temp;
temp = get_next<iterator, T>(temp);
}
return end;
}
template<class iterator, class T>
void traverseList(iterator start, iterator end)
{
iterator temp = start;
while (temp != end)
{
cout << get_data<iterator,T>(temp) << " ";
temp = get_next<iterator, T>(temp);
}
cout << endl;
}
template<class iterator, class T>
T get_data(iterator current_ptr)
{
return current_ptr->data();
}
template<class iterator, class T>
iterator get_next(iterator current_ptr)
{
return current_ptr->link();
}
//end of sample.cpp
Output: