Sunday, 15 September 2013

Communication and using Methods between Header and CPP files in C++

Communication and using Methods between Header and CPP files in C++

I've got a header file named ArrayLinearList.h and a Main.cpp file for a
project I'm working on. I declare and initialize all methods and
constructors in the Header file for use in my main method. I'm confused as
to the communication between these files, specifically when using
templates. Why don't any of my three method calls in main.cpp work? They
all say that the methods or classes are not delcared. My code is below.
ArrayLinearList.h
//Header file
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <ostream>
#include <string>
#include <list>
using namespace std;
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& get(int theIndex) const = 0;
virtual int indexOf(const T& theElement)const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex, const T& theElement) = 0;
virtual void output(ostream& out) const = 0;
};
template<class T>
class arrayList : public linearList<T>
{
public:
arrayList(int initialCapacity=10);
arrayList(const arrayList<T>&);
~arrayList() {delete[] element;}
bool empty() const {return listSize==0;}
int size() const {return listSize;}
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;
int capacity() const {return arrayLength;}
void changeLength1D(T*&a, int oldLength, int newLength);
protected:
void checkIndex(int theIndex) const;
//Throw error if invalid Brett
T* element; //One dimensional array
int arrayLength; //Max capacity of array
int listSize; //Current # of elements in array
T* temp;
int number;
};
//First Constructor
template <class T>
arrayList<T>::arrayList(int initialCapacity)
{
//Constructor
if (initialCapacity<1)
{
// throw error Brett
}
arrayLength=initialCapacity;
element = new T[arrayLength];
listSize=0;
}
//Copy Constructor
template <class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
arrayLength=theList.arrayLength;
listSize=theList.listSize;
element=new T[arrayLength];
copy(theList.element,theList.element+listSize,element);
}
template <class T>
void arrayList<T>::checkIndex(int theIndex) const
{
//Check that index is between 0 and listsize-1
if(theIndex<0 || theIndex >=listSize)
{
//Brett
}
}
template <class T>
T& arrayList<T>::get(int theIndex) const
{
//Return element at index, throw exception if illegal
checkIndex(theIndex);
return element[theIndex];
}
template <class T>
int arrayList<T>::indexOf(const T& theElement) const
{
//Return index of first occurence of theElement
int theIndex=(int) (find(element, element+listSize,
theElement)-element);
//Check if it was found
if(theIndex==listSize)
return -1;
else return theIndex;
}
template <class T>
void arrayList<T>::erase(int theIndex)
{
//Delete element at theIndex
checkIndex(theIndex);
//If a valid index, shift elements with higher index
copy(element+theIndex+1, element+listSize, element+theIndex);
element[--listSize].~T; //Destructor
}
template <class T>
void arrayList<T>::insert(int theIndex, const T& theElement)
{
//Insert the eleemnt at valid index
checkIndex(theIndex);
//Check for space
if(listSize==arrayLength)
{
//no space, double the capacity
changeLength1D(element, arrayLength, 2*arrayLength);
arrayLength *=2;
}
//shift elements right one position
copy_backward(element +theIndex, element+listSize,
element+listSize+1);
element[theIndex] = theElement;
listSize++;
}
template <class T>
void arrayList<T>::changeLength1D (T*&a, int oldLength, int newLength)
{
if (newLength<0)
//Brett
T* temp = new T[newLength];
int number = min(oldLength,newLength);
copy(a, a+number, temp);
delete[] a;
a=temp;
}
/* template<class T>
void arrayList<T>::output(ostream& out) const
{
//Put the list into the stream out
copy(element, element+listSize,ostream_iterator<T>(out," "));
}
template <class T>
{
ostream& operator<<(ostream& out, const arrayList<T>& x)
{x.output(out); return out;}
}
*/
Main.cpp
//Main.cpp file
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include "ArrayLinearList.h"
using namespace std;
int main()
{
ArrayLinearList::arrayList(); //Call constructor?
ArrayLinearList:: checkIndex(3); //Check index of constructor-made
list
arrayList::arrayList();
arrayList::erase(3);
}
The errors I am getting:
g++ main.cpp -o run
main.cpp: In function 'int main()':
main.cpp:22: error: 'ArrayLinearList' has not been declared
main.cpp:23: error: 'ArrayLinearList' has not been declared
main.cpp:24: error: 'template<class T> class arrayList' used without
template parameters
main.cpp:25: error: 'template<class T> class arrayList' used without
template parameters
Any help or pointers will be much appreciated
-Brett

No comments:

Post a Comment