Ok I have a header file that was given to us by the instructor. He told us to make it a template and then make a little prog to test it....
He uses a "typedef" to define the element type that is passed through the prog, how to I make it into a template so that it will change from "int" to "Float" what ever I want to pass through this prog.
here is the Templated Header file as I have it right now...
Code:
// TEMPLATE LIST HEADER FILE
#ifndef _LIST
#define _LIST
const int CAPACITY = 100;
template <class T> T ElementType; //I know this doesnt work
//typedef std::map<std::string, T> ElementType; I found this on a web page
// but I cant figure out how they use it to get around templating a typedef
template <class T> class List
{
public:
// constructors
List(); // def constructor
List(const List &l); // copy constructor
// public member functions
int get_size() const; // returns logical size of array
List &operator = (const List &l); // assign arrays
ElementType &operator[](int index);
// returns element index of array can extract ith element and
// assign ith element. Index must be valid
void sort();
bool append(const ElementType &tem);
// Appends element to end returns true if successful
// otherwise false
bool insert(const ElementType &item, int pos);
// inserts item at pos, if pos is valid. returns true if LIst is
// not full and pos is valid, otherwise false
bool erase(int pos);
// if pos >= 0 and pos < current_size item as position pos
// is removed from the array.
bool empty() const;
// tests to see if List is empty
void traverse(void process(const ElementType & item));
// discussed in class
protected:
ElementType data[CAPACITY];
int current_size;
};
#endif
the error I get is this
Code:
In file included from List.cpp:2:
List.h:6: error: template declaration of `T ElementType'
I believe that is sayig I cant template a typedef, If I am wrong pls tell me how to make this statement work..I believe that this is all that is holding my prog from working and I cannot find anything in the book as it is pretty worthless (so says my proffessor)...
Thanks in advance