thanx, i guess i can blame this one on Microsoft...nice...
everything compiles and links fine, however the program crashes, ....the program is all nice and good (logically) as far as I'm concerned..... my eyes are done for today...
if any of you would like to take a peek be my guest,
the following are 3 small source code files:
maybe you can run it....or maybe tomorrow I will say Hi again to Borland...
Code:
................
// List.h (class template)
//---------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#ifndef LIST_H
#define LIST_H
template<class ItemType>
class List
{
public:
List(int max);
// Initializes the object to an empty list that can hold up to the 'max'
// items of the type 'ItemType' ...
~List();
// Returns all the dynamically allocated memory to the freestore.
int length() const;
// Returns the number of items in the list.
void add(ItemType new_item);
// Preconditions: List must NOT be full!!!
// Postconditions: The 'new_item' has been added to the list.
bool full() const;
// Returns 'true' if the list is full and 'false' if the list is not.
void erase();
// Removes all the items from the list so the list is empty...
friend ostream& operator <<(ostream& outs, const List<ItemType>& the_list)
{
for(int i = 0; i < the_list.current_length; i++)
outs << the_list.item[i] << endl;
return outs;
}
// Overloads the operator '<<" so that the contents of the List can
// be outputed
private:
ItemType *item; // pointer to the dynamically allocated array
int max_length; // max number of items allowed in the list
int current_length; // number of items currently in the list
};
#endif
now it's implementation:
Code:
// List.cpp
//---------------------------------------------------------------------------------------
#include<iostream>
#include<cstdlib>
using namespace std;
template<class ItemType>
List<ItemType>::List(int max): max_length(max)
{
item = new ItemType[max];
}
template<class ItemType>
List<ItemType>::~List()
{
delete [] item;
}
template<class ItemType>
int List<ItemType>::length() const
{
return current_length;
}
template<class ItemType>
void List<ItemType>::add(ItemType new_item)
{
if( full() )
{
cout << "The LIST is full. Exiting the program!!! \n";
exit(1);
}
else
{
item[current_length] = new_item;
current_length++;
}
}
template<class ItemType>
bool List<ItemType>::full() const
{
return (current_length == max_length);
}
template<class ItemType>
void List<ItemType>::erase()
{
current_length = 0;
}
//---------------------------------------------------------------------------------------
and finally a tiny 'main' function
Code:
// driver function...
//---------------------------------------------------------------------------------------
#include<iostream>
#include"List.h"
#include"List.cpp"
using namespace std;
int main()
{
List<int> int_list(2);
int_list.add(2);
int_list.add(4);
cout << "List of Integers: \n";
cout << int_list;
List<char> char_list(3);
char_list.add('A');
char_list.add('B');
char_list.add('C');
cout << "\nList of Characters: \n";
cout << char_list;
return 0;
}
//---------------------------------------------------------------------------------------
have fun....
sorry for a long post...
matheo917