---------------------------------------------TSet.h------------------------------------------------------
#include <iostream>
using namespace std;
template <class V>
class TSet
{
public:
TSet();
TSet(int size);
~TSet();
void add(V new_item);
void remove(V item_to_remove);
int num_of_items();
bool isFull();
bool is_item_in_set(V item);
friend bool operator==(const TSet<V>& left, const TSet<V>& right);
friend bool operator!=(const TSet<V>& left, const TSet<V>& right);
friend ostream& operator<<(ostream& outs,const TSet<V>& list);
private:
V *item_list;
int size_of_list;
int current_size;
};
----------------------------------------TSet.cpp-----------------------------------------
#include <iostream>
#include "TSet.h"
using namespace std;
template<class V>
TSet<V>::TSet() : size_of_list(0), current_size(0)
{ item_list = new V[0]; }
template<class V>
TSet<V>::TSet(int size) : size_of_list(size), current_size(0)
{ item_list = new V[size]; }
template<class V>
TSet<V>::~TSet()
{ delete [] item_list; }
template<class V>
void TSet<V>::add(V new_item)
{
if(isFull())
cout << "Unable to add element " << new_item << " to set that is full.\n";
else
{
item_list[current_size] = new_item;
current_size = current_size + 1;
cout << new_item << " was added to the set." << endl;
}
}
template<class V>
void TSet<V>::remove(V item_to_remove)
{
for(int i = 0; i < current_size; i++)
{
if(item_list[i] == item_to_remove)
{
item_list[i] = NULL;
cout << "Successfully removed " << item_to_remove << " from the set.\n";
for(int j = i; i <= current_size; i++)
{
item_list[i] = item_list[i+1];
currentSize = currentSize - 1;
}
}
}
}
template<class V>
int TSet<V>::num_of_items()
{
int element_count;
for(int i = 0; i < size_of_list)
{
if(item_list[i] != NULL)
{ element_count++; }
}
return element_count;
}
template<class V>
bool TSet<V>::isFull()
{
if(current_size == size_of_list)
return true;
return false;
}
template<class V>
bool TSet<V>::is_item_in_set(V item)
{
for(int i = 0; i < current_size; i++)
{
if(item_list[i] == item)
{ return true; }
}
return false;
}
template<class V>
bool operator==(const TSet<V>& left, const TSet<V>& right)
{
return left == right;
}
template<class V>
bool operator!=(const TSet<V>& left, const TSet<V>& right)
{
return !(left == right);
}
template<class V>
ostream& operator<<(ostream& outs,const TSet<V>& list)
{
for(int i = 0; i < list.current_size; i++)
{ outs << list.item_list[i] << " "; }
return outs;
}
------------------------------------TSet_Driver.cpp-------------------------------------------------------
#include <iostream>
#include "TSet.cpp"
using namespace std;
int main()
{
TSet<int> int_list(5);
for(int i = 1; i <= 5; i++)
{ int_list.add(i); }
cout << "int_list: " << int_list << endl;
int_list.add(6);
system("Pause");
return 0;
}
-----------------------------------------------------------------------------------------------------------
When I compile this Microsoft Visual C++ 2010 Express. I get this:
1>------ Build started: Project: TSet, Configuration: Debug Win32 ------
1> TSet_Driver.cpp
1> LINK : C:\Users\name\Documents\Dropbox\TSet\Debug\TSet.exe not found or not built by the last incremental link; performing full link
1>TSet_Driver.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class TSet<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$TSet@H@@@Z) referenced in function _main
1>C:\Users\name\Documents\Dropbox\TSet\Debug\TSet.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What did I mess up here? Any answers?