I include my implementation at the end of the header so its included before the compiler is done with the .h file. but i have one error reffering to the .cpp file.
I'm trying to pull an implementation file from the template its one of the requirements for my assignment. It's done in my texts, but i cant make this work properly. Maybe some insight would help. and can i pull anything from the header and move it to my imp file? thanks in advanced everyone.
//linkedlist.cpp
template<class dataType>
void LinkedList<dataType>:: add(const dataType& data){//@add adds param of classes datatype to the list
Node<dataType>* n=new Node<dataType>;
n->data=data;
n->next=headPointer;
headPointer=n;
}
//linkedlist.h
/*
LinkedList.h
This is the implementation for a templated LinkedList that contains the ability
to add(to the front), remove(from the front), clear, and peek at the head entry
of the list.
*/
#include<iostream>
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template<class dataType>
struct Node{
dataType data;
Node * next;
};
template<class dataType>
class LinkedList
{
public:
LinkedList(){//@constructor, initiallizes an empty list.
headPointer=NULL;
};
~LinkedList(){clear();};//@destructor deallocates a list
void add(const dataType&);//@add adds param of classes datatype to the list
dataType remove(){//@remove ifthe list is not empty this removes the head, and returns it's value
if(!isEmpty()){
Node<dataType>* temp=headPointer;
dataType d=temp->data;
headPointer=headPointer->next;
delete temp;
return d;
}
elsereturn NULL;
}
void clear(){//@clear, deletes entire list
while(!isEmpty()){
std::cout<<remove()<<", ";
}
};
bool isEmpty() const{//@isEmpty checks if list is empty
return headPointer==NULL;
};
dataType peek() const{//@peek returns the head bit of data
if(!isEmpty())
return headPointer->data;
elsereturn NULL;
};
void print() const{
Node<dataType>* cur=headPointer;
while(cur!=NULL){
std::cout<<"\n"<<cur->data<<"\n";
cur=cur->next;
}
}
private:
Node<dataType>* headPointer;
};
#include "LinkedList.cpp"
#endif // LINKEDLIST_H
I need to have a separate implementation is what im saying basically, I'm having trouble moving functions from the .h to the .cpp. and i feel the definitions directly in the .cpp is kinda sloppy anyways so for my own sake i'd like to move the function implementations to another file