typedef syntax to get started on the template.
You'll be making a template allowing you to create
a table mapping strings to ints, ints to strings, ints to Robots, or
whatever. The main rule is that the indexing class must have
the comparators <, >,
and ==.
StringToIntTable.cc and
StringToIntTable.h and give them more appropriate (and
general) names like, Table.cc and Table.h.
StringToIntTable with
Table in all files.
= 0 in the class declaration of
Table. (Dealing with this optional argument in
templates is a bit anoying.) Be sure you never assigned the initial
value of = 0 in your code. To get your code to compile,
you'll need to change the declaration at the top of main to:
Table table(0);
typedef's. One will be the Key
(currently of class string), and one will be the
Data (currently of class int). For reference, you
may wish to look at the List class definitions from class in
~mc38/labs/operator-overloading/files/List.*Be sure the code compiles and runs.
#include <iostream>
#include <string>
#include "Table.h"
int main() {
Table; table (" 0 ");
table[20] = " 1 ";
table[10] = " 2 " ;
table[-100] = " 3 ";
table[10] = " 4 ";
table[-100] = " 5 ";
cout << table[20] << table[10] << table[-100] << table[30] << endl;
return 0;
}
List
class is in the directory ~mc38/labs/templates/list/. A
few things are worth noting.
Table.h and Table.cc files will
need to be edited. Any program using these classes will need to be
changed only slightly.
<class Key, class Data>
List.h file ends with the lines
#include "List.cc" #endif LIST_H
template<class ListItem>Both the
List class and the Node class are templated.
List<ListItem>::methodName
Node or List variable declaration
(whether they be parameters, local variables or instance variables) is
replaced by the template:
Node<ListItem> varName List<ListItem> varName
Makefile so that it no longer compiles
Table.cc. Only your file with your main should
be compiled.
Table<int, string> table (" 0 "); // and
Table<string, int> table (" 0 ");