I have an assignment where I have to implement the abstract data type (ADT) Set which defines a set.
The following applies to sets:
There must only be one occurrence of each element in the set (no duplicates allowed)
The elements in the set are not ordered (they are not in a particular order)
The union of two sets A and B (A U B) results in a new set containing the same elements as in A and also the element that are in B, but no duplicates.
The difference between two sets A and B (A - B) results in a new set containing the elements that are in A but not on B.
The intersection of the sets A and B produces a new set which contains all the elements that are equal in sets A and B.
The set is described by:
template <class T>
class Set
{
private:
class Node
{
public:
T data;
Node *next;
Node(T data) { this->data = data; this->next = NULL;}
~Node(){}
};
Node *first;
int nrOfNodes;
public:
Set();
Set(const Set<T>& set);
virtual ~Set();
void insert(T item);
T remove(T item); // returnerar det element som tas bort
bool isMember(const T& item) const;
int size() const;
Set<T> operator=(const Set<T>& set);
Set<T> operator+(const Set<T>& set) const; // union
Set<T> operator-(const Set<T>& set) const; // differense
Set<T> operator*(const Set<T>& set) const; // snitt
};
The storage structure for the set is a single-linked circular list (the next-pointer of the last element points at the first element).
I have done the followng codes so far, some of the functions are empty for easy compiling etc.
//search
template <class T>
bool Set<T>::isMember(const T& item) const
{
// return index if found or -1 if not found
Node* p;
int count = 0;
for (p = front; p != NULL; p = p->link)
{
if (p->data == item)
{
return true;
}
count++;
}
return false;
}
//add
template <class T>
void Set<T>::insert(T item)
{
}
//remove
template <class T>
T Set<T>::remove(T item)
{
}
//Display
template <class T>
void Set<T>::display()
{
Node *q;
void main()
{
Set<int> SetA(10);
Set mySet(SetA);
}
When I compiled my codes above I get these error
cannot convert parameter 1 from 'int' to 'const Set<T> &'
with
[
T=int
]
Reason: cannot convert from 'int' to 'const Set<T>'
with
[
T=int
]
No constructor could take the source type, or constructor overload resolution was ambiguous
d:\visualprogrammes\linkedlist\linkedlist\linked_list.cpp(73) : error C2514: 'Set' : class has no constructors
d:\visualprogrammes\linkedlist\linkedlist\linked_list.cpp(6) : see declaration of 'Set'
So my question is how do I use these functions defined in the template <class T> class Set
Set();
Set(const Set<T>& set);
virtual ~Set();
Can you one please help how to use it or if possible also some codes which illustrates how it canbe use.
Thank you very much and appreciate your help!