/*The key class keeps track of comparisons of an algorithm by overloading operators, and incrementing a counter. Templated so, it can take any standard datatype. */ template class Key { public: static double comparisons; Key (T x=0); T the_key() const; friend bool operator == (const Key &x,const Key &y); friend bool operator > (const Key &x,const Key &y); friend bool operator < (const Key &x,const Key &y); friend bool operator >= (const Key &x,const Key &y); friend bool operator <= (const Key &x,const Key &y); friend bool operator != (const Key &x,const Key &y); T operator = (const Key &x,const Key &y); //To be friend or not to be friend. //friend ostream& operator << (ostream& outs, const Key &x); //Precondition: if outs is a file output stream, //it has already been connected to a file //Overloads << operator to output values of type Key private: T key; //Key is a templated type }; /* hmm doesn't work ERROR: c:\Stuff\School\Visual Studio Projects\Prog6\key.h(20) : error C2143: syntax error : missing ';' before '<' c:\Stuff\School\Visual Studio Projects\Prog6\key.h(30) : see reference to class template instantiation 'Key' being compiled c:\Stuff\School\Visual Studio Projects\Prog6\key.h(20) : error C2238: unexpected token(s) preceding ';' c:\Stuff\School\Visual Studio Projects\Prog6\key.h(39) : error C2801: 'operator =' must be a non-static member */ template T operator = (const Key &x, const Key &y) { Key::comparisons++; return y.the_key(); } template double Key::comparisons=0;//This seems like a weird place for this. template ostream& operator << (ostream& outs, const Key &x) { return outs << x.the_key(); } template Key::Key(T x) { key = x; } template T Key::the_key() const { return key; } template bool operator == (const Key &x, const Key &y) { Key::comparisons++; return x.the_key() == y.the_key(); } template bool operator > (const Key &x, const Key &y) { Key::comparisons++; if (debug > 0) cout << ">"; return x.the_key() > y.the_key(); } template bool operator < (const Key &x, const Key &y) { Key::comparisons++; if (debug > 0) cout << "<"; return x.the_key() < y.the_key(); } template bool operator >= (const Key &x, const Key &y) { Key::comparisons++; return x.the_key() >= y.the_key(); } template bool operator <= (const Key &x, const Key &y) { Key::comparisons++; return x.the_key() <= y.the_key(); } template bool operator != (const Key &x, const Key &y) { Key::comparisons++; return x.the_key() != y.the_key(); }