simple list and driver program help
this lil piece of code has been driving me nuts, it's just a simple list that can insert and remove strings into an array. please someone tell me where a stupid error is becuase i can't find it.
in case your wondering about the DEL declaration when i tried to set an element of myArray to NULL in teh removeString method it gave me an ambious overload error of =.
Code:
#ifndef LIST
#define LIST
#include <iostream>
#include <string>
using namespace std;
const int CAPACITY = 10;
typedef string ElementType;
const string DEL = NULL;
class List
{
public:
List();
/*----------------------------------------------------------------------
Construct a List object.
Precondition: None
Postcondition: An empty List object has been constructed.
-----------------------------------------------------------------------*/
bool empty() const;
/*----------------------------------------------------------------------
Check if a list is empty.
Precondition: None
Postcondition: true is returned if the list is empty, false if not.
-----------------------------------------------------------------------*/
int sizeOf() const;
/*----------------------------------------------------------------------
Find the number of items in a list.
#include "List.h"
using namespace std;
int main () {
List myList;
try {
myList.insertString("DATA STRUCTURES");
cout <<" 1 "<< endl;
myList.insertString("CONSTRUCTORS");
myList.insertString("DESTURCTORS");
myList.insertString("TEMPLATES");
myList.insertString("COPY CONSTRUCTOR");
myList.insertString("EXPLICIT VALUE CONSTRUCTOR");
myList.insertString("MEMORY LEAKS");
myList.insertString("A QUICK BROWN FOX");
myList.insertString("JUMPS OVER THE LAZY DOG");
}
catch (string error) {
cout << error << endl;
}
cout << myList;
return 0;
}
Precondition: None
Postcondition: returns the number of items in the list.
-----------------------------------------------------------------------*/
void insertString(ElementType item) throw (string);
/*----------------------------------------------------------------------
Insert an item into the list at a given position.
Precondition: item is the value to be inserted; there is room in
the list; pos is >= 0 and <= sizeOf()the list.
Postcondition: item has been inserted into the list at the position
determined by pos (provided there is room and pos is a legal
position).
-----------------------------------------------------------------------*/
void removeString(ElementType item) throw(string);
/*----------------------------------------------------------------------
Remove an item from the list at a given position.
Precondition: The list is not empty; pos is >= 0 and < sizeOf() the list.
Postcondition: item at the position determined by pos has been
removed (provided pos is a legal position).
----------------------------------------------------------------------*/
friend std::ostream & operator << (std::ostream& out, const List & f);
/*---------------------------------------------------------------
Overloaded Output Operator.
Precondition: The ostream out is open.
Postcondition: The fraction represented by this Fraction
object has been placed into out and reference
to out is returned.
---------------------------------------------------------------*/
private:
int mySize; // current size of list stored in myArray
ElementType myArray[CAPACITY]; // array to store list items
}; //--- end of List class
#endif
Code:
#include "List.h"
using namespace std;
List::List(): mySize(0) {}
bool List::empty() const {
return mySize == 0;
}
int List::sizeOf() const {
return mySize;
}
void List::insertString(ElementType item) throw (string) {
cout << "hi" << endl;
string errorString;
if (mySize == CAPACITY) {
errorString = "The List capacity has been reached. Cannot add new elements.";
throw errorString;
}
int i = 0;
while (item.size() > myArray[i].size()) {
i ++;
}
for(int x = mySize; x > i; x --) {
myArray[x+1] = myArray[x];
}
myArray[i] = item;
mySize++;
}
void List::removeString(ElementType item) throw(string)
{
string errorString;
if (mySize == 0)
{
errorString = "The List is empty. Cannot remove string.";
throw errorString;
}
int i = 0;
while (item.size() > myArray[i].size()) {
i ++;
}
for(int x = i; x < mySize; x ++) {
myArray[i] = myArray[i + 1];
}
myArray[mySize] = DEL;
mySize--;
}
ostream & operator << (ostream & out, const List & temp) {
if (temp.mySize == 0) {
out << "List is empty" << endl;
}
else {
for (int x = 0 ; x < temp.mySize ; x ++) {
out << (x + 1) << ") " << temp.myArray[x] << endl;
}
}
out << endl;
return out;
}
Code:
#include "List.h"
using namespace std;
int main () {
List myList;
try {
myList.insertString("DATA STRUCTURES");
cout <<" 1 "<< endl;
myList.insertString("CONSTRUCTORS");
myList.insertString("DESTURCTORS");
myList.insertString("TEMPLATES");
myList.insertString("COPY CONSTRUCTOR");
myList.insertString("EXPLICIT VALUE CONSTRUCTOR");
myList.insertString("MEMORY LEAKS");
myList.insertString("A QUICK BROWN FOX");
myList.insertString("JUMPS OVER THE LAZY DOG");
}
catch (string error) {
cout << error << endl;
}
cout << myList;
return 0;
}
makefile if you need it
Code:
#specify the compiler
GXX=g++ -g
# Specifiy the target
all: ListDriver.exe
# Specify the object files that the target depends on
# Also specify the object files needed to create the executable
ListDriver.exe: ListDriver.o List.o
$(GXX) ListDriver.o List.o -o ListDriver.exe
# Specify how the object files should be created from source files
ListDriver.o: ListDriver.cpp
$(GXX) -c ListDriver.cpp
List.o: List.cpp
$(GXX) -c List.cpp
# Specify the object files and executables that are generated
# and need to be removed to re-compile the whole thing
clean:
rm -f *.o *~ core ListDriver.exe