Question

In: Computer Science

this program shows an error that Error (active)   E0308   more than one instance of overloaded function...

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

Solutions

Expert Solution

// 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:


Related Solutions

Would one, for instance, prefer: - to have a type I error in which case further...
Would one, for instance, prefer: - to have a type I error in which case further testing will be conducted, extra time and money spent or - to have a type II error in which case no treatment will be administered, disease will develop in time, maybe even make other people sick? What do you think?
Why does it not make sense for a function to have more than one output for...
Why does it not make sense for a function to have more than one output for the same input? Provide examples. First , define inputs and outputs ( independent and dependent variables) . Then, define ordered pairs and relations and give examples. Finally, define a function and talk about what particular type of relation is a function. Can you find a real world relation that is not a function? For example: The age of a person is the input and...
If a linear program has more than one optimal solution, does this mean that it doesn’t...
If a linear program has more than one optimal solution, does this mean that it doesn’t matter which solution is selected?
Will a highly active muscle receive more, same or less oxygen than a resting muscle? Explain...
Will a highly active muscle receive more, same or less oxygen than a resting muscle? Explain the mechanism that may influence oxygen delivery to this muscle. Appropriate diagrams/graphs describing relevant hemoglobin properties are required in order to receive full credit.
Creating an innovative organization requires more than understanding the design of an efficient new product development process more than how to write innovative into company strategy document and more than maintaining an active research and development.
Creating an innovative organization requires more than understanding the design of an efficient new product development process more than how to write innovative into company strategy document and more than maintaining an active research and development. Innovation design and creativity need to permeate every aspect of an organization. It is of the utmost importance to be aware that creating a more innovative organization is much more about changing one’s frame of mind than it is about a changing the company’s...
Is carrying more than one EpiPen advisable?
Is carrying more than one EpiPen advisable?
For small training sets variance may contribute more to the overall error than bias. Sometimes this...
For small training sets variance may contribute more to the overall error than bias. Sometimes this is handled by reducing the complexity of the model, even if the model is too simple. Why do you suppose this is the case? Come up with your own example of this
Discuss why an organization may decide to target more than one market segment. If more than...
Discuss why an organization may decide to target more than one market segment. If more than one segment is targeted, what challenges do you think may exist?   
Why might an active policy approach be more politically popular than a passive approach, especially during...
Why might an active policy approach be more politically popular than a passive approach, especially during a recession?
1. The Medicare program is more likely to suffer from adverse selection than the Medicaid program.
For each of the following, answer True or False. If false, very briefly explain why it is false. 1. The Medicare program is more likely to suffer from adverse selection than the Medicaid program. 2. Suppose the state grants Medicaid benefits to a worker only if his income is between $5,000 and $10,000 per year. This will not result in a work disincentive because those who are not working at all cannot receive any benefits. 3. The primary sources of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT