Question

Was converting the main into a template but I am recieving errors, what should I be...

Was converting the main into a template but I am recieving errors, what should I be doing?

#include<iostream>

#include <cstdlib>

#include "set.h"

using namespace std;

int main()

{

   set<Item> list;

   list.insert(4);

   list.insert(7);

   list.insert(9);

   list.insert(3);

cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl;

   list.erase(4);

cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl;

}


#ifndef SET_H
#define SET_H
#include<cstdlib>

namespace std
{
   template<class Item>
class set_item
{
public:
typedef Item value_type;
set_item( ) { next = 0; }
value_type value;
set_item* next;
};

   template<class Item>
class set
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef Item value_type;
typedef std::size_t size_type;
// CONSTRUCTOR
set( ) { data = 0; }
set(const set& s);
~set( );
// MODIFICATION MEMBER FUNCTIONS
bool erase(const Item& target);
void insert(const Item& entry);
set& operator =(const set& s);
void operator +=(const set& s);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool contains(const Item& target) const;
private:
set_item<Item>* data; // The list to store items
};

// NONMEMBER FUNCTIONS for the set class
   template<class Item>
set<Item> operator +(const set<Item>& s1, const set<Item>& s2);
}

#endif

#include <algorithm> // Provides copy function

#include <cassert> // Provides assert function

#include "set.h"

using namespace std;

template <class Item>

set<Item>::set(const set& s)

{

data = 0;

set_item<Item>* sitem = s.data;

while (sitem)

{

set_item* item = data;

data = new set_item( );

data->value = sitem->value;

data->next = item;

sitem = sitem->next;

}

}

template <class Item>

set<Item>::~set( )

{

while (data)

{

set_item* item = data;

data = data->next;

delete item;

}

}

template <class Item>

bool set<Item>::erase(const value_type& target)

{

set_item<Item>* prev = 0;

set_item<Item>* item = data;

while (item)

{

if (item->value == target)

{

if (prev)

{

prev->next = item->next;

delete item;

}

else

{

data = item->next;

delete item;

}

return true;

}

item = item->next;

}

return false;

}

template <class Item>

void set<Item>::insert(const value_type& entry)

{

if (contains(entry))

return;

set_item* item = data;

data = new set_item( );

data->value = entry;

data->next = item;

}

template <class Item>

set& set<Item>::operator =(const set& s)

{

while (data)

{

set_item* item = data;

data = data->next;

delete item;

}

data = 0;

set_item<Item>* sitem = s.data;

while (sitem)

{

set_item* item = data;

data = new set_item( );

data->value = sitem->value;

data->next = item;

sitem = sitem->next;

}

return *this;

}

template <class Item>

void set<Item>::operator +=(const set& s)

{

set_item<Item>* item = s.data;

while (item)

{

insert(item->value);

item = item->next;

}

}

template <class Item>

set<Item>::size_type set<Item>::size( ) const

{

size_type answer = 0;

set_item<Item>* item = data;

while (item)

{

++answer;

item = item->next;

}

return answer;

}

template <class Item>

bool set<Item>::contains(const value_type& target) const

{

set_item<Item>* item = data;

while (item)

{

if (item->value == target)

return true;

item = item->next;

}

return false;

}

set operator +(const set& s1, const set& s2)

{

set answer;

answer += s1;

answer += s2;

return answer;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You used set class every where in the program wich was not defiend. in program....firsly program will fails in the statement #include "set.h"

So you have to provide set.h header..then ot will works fine

#include<iostream>
#include <cstdlib>

#include<list>
//#include "set.h"
using namespace std;


#ifndef SET_H
#define SET_H
#include<cstdlib>
namespace std
{
template<class Item>
class set_item
{
public:
typedef Item value_type;
set_item( ) { next = 0; }
value_type value;
set_item* next;
};
template<class Item>
class set
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef Item value_type;
typedef std::size_t size_type;
// CONSTRUCTOR
set( ) { data = 0; }
set(const set& s);
~set( );
// MODIFICATION MEMBER FUNCTIONS
bool erase(const Item& target);
void insert(const Item& entry);
set& operator =(const set& s);
void operator +=(const set& s);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool contains(const Item& target) const;
private:
set_item<Item>* data; // The list to store items
};
// NONMEMBER FUNCTIONS for the set class
template<class Item>
set<Item> operator +(const set<Item>& s1, const set<Item>& s2);
}
#endif
#include <algorithm> // Provides copy function
#include <cassert> // Provides assert function
//#include "set.h"
using namespace std;

template <class Item>
set<Item>::set(const set& s)
{
data = 0;
set_item<Item>* sitem = s.data;
while (sitem)
{
set_item* item = data;
data = new set_item( );
data->value = sitem->value;
data->next = item;
sitem = sitem->next;
}
}

template <class Item>
set<Item>::~set( )
{
while (data)
{
set_item* item = data;
data = data->next;
delete item;
}
}
template <class Item>
bool set<Item>::erase(const value_type& target)
{
set_item<Item>* prev = 0;
set_item<Item>* item = data;
while (item)
{
if (item->value == target)
{
if (prev)
{
prev->next = item->next;
delete item;
}
else
{
data = item->next;
delete item;
}
return true;
}
item = item->next;
}
return false;
}
template <class Item>
void set<Item>::insert(const value_type& entry)
{
if (contains(entry))
return;
set_item* item = data;
data = new set_item( );
data->value = entry;
data->next = item;
}
template <class Item>
set& set<Item>::operator =(const set& s)
{
while (data)
{
set_item* item = data;
data = data->next;
delete item;
}
data = 0;
set_item<Item>* sitem = s.data;
while (sitem)
{
set_item* item = data;
data = new set_item( );
data->value = sitem->value;
data->next = item;
sitem = sitem->next;
}
return *this;
}

template <class Item>
void set<Item>::operator +=(const set& s)
{
set_item<Item>* item = s.data;
while (item)
{
insert(item->value);
item = item->next;
}
}

template <class Item>
set<Item>::size_type set<Item>::size( ) const
{
size_type answer = 0;
set_item<Item>* item = data;
while (item)
{
++answer;
item = item->next;
}
return answer;
}

template <class Item>
bool set<Item>::contains(const value_type& target) const
{
set_item<Item>* item = data;
while (item)
{
if (item->value == target)
return true;
item = item->next;
}
return false;
}

set operator +(const set& s1, const set& s2)
{
set answer;
answer += s1;
answer += s2;
return answer;
}
  
int main()
{
set<Item> list;
list.insert(4);
list.insert(7);
list.insert(9);
list.insert(3);
cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl;
list.erase(4);
cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl;
}

Add a comment
Know the answer?
Add Answer to:
Was converting the main into a template but I am recieving errors, what should I be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides...

    Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

  • c++ Sequence class is a class that supports sequence of integers. Run the project ad verify...

    c++ Sequence class is a class that supports sequence of integers. Run the project ad verify that project works for integers. Next convert your sequence class into a template class. Demonstrate in your main function by using a sequence of int, float, string. sequence.cxx #include // Provides assert using namespace std; namespace main_savitch_3 { sequence::sequence() { used = 0; current_index = 0; } void sequence::start() { current_index = 0; } void sequence::advance() // Library facilities used: assert.h { assert(is_item()); current_index++;...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT