A C++ program.

I have a lists.cpp file that contains 2 classes, a Link class (which creates the nodes) and a LinkedList class to create linked lists using the Link class.  I have a .h file that includes the prototypes for all the functions I want to use on my linked lists, for example, append, next, getValue, etc.  I want to write a separate driver.cpp to check the various functions are working correctly.  I understand that I should include the .h file in the driver.cpp file. I was told to compile the lists.cpp file and the driver.cpp file separately, both including the .h file and then link them together using the following code:  g++ -c -o lists.o lists.cpp, g++ -c -o driver.o driver.cpp, and g++ -o exec lists.o driver.o.  Here is my question:  How can I create a linked list (LinkedList* actlist = new LinkedList) in the driver.cpp when classes are not included in the .h file?  I need a linked list in order to check that the functions are working.
Who is Participating?
Your lists.h file should also contain the declarations for the Link and LinkedList classes. Does it not?
QRFOAAuthor Commented:
It doesn't and I don't want to add those to that file.  I have tried making another .h file (called myClasses) with that information, but it doesn't seem to be able to piece the two .h files together.
You need to make this file with declarations. Please show us the code that is failing to compile and we can help you. You need to declare the classes from the base to parents, i.e. if your Link class is the base of LinkedList it needs to appear in the h file first. If there is a cross reference between these classes then you need to use a technique called  "forward declaration". Just show us a small example and we will help
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

QRFOAAuthor Commented:
The beginning of the List.h file looks like the following with the remaining part just functions like the clear().
template<typename E> class List {
   void operator =(const List&) {}  // Protect assignment
   List(const List&) {}    // Protect copy constructor
   List() {}  // Default constructor
   virtual  ~List() {} // Base destructor

   // Clear contents from the list, freeing memory
   virtual void clear() = 0;

Open in new window


The lists.cpp file has 2 classes, Link and LinkedList. The first part of the file is below:

template <typename E> class Link {

	static Link<E>* freelist;  //Pointer to freelist head
	static int freeNodes;  //Number of nodes in freelist
	static int activeNodes;  //Number of active nodes
	E element;  //Value for the node
	Link* next;

	Link (const E& elemval, Link* nextval = NULL)
		{element = elemval; next = nextval;}
	Link (Link* nextval = NULL) {next = nextval;}
	~Link() {} // Base destructor

void* operator new(size_t) { //Overload new operator
		if (freelist == NULL) {
			activeNodes = activeNodes + 1;
			return :: new Link; //New node
		Link<E>* temp = freelist; //Otherwise can take from freelist
		freelist = freelist->next;
		freeNodes = freeNodes - 1;
		activeNodes = activeNodes + 1;
		return temp;
	void operator delete (void* ptr) {
		((Link<E>*)ptr)->next = freelist;  //Put on freelist
		freelist = (Link<E>*)ptr;
		freeNodes = freeNodes + 1;
		activeNodes = activeNodes - 1;


	template <typename E> int Link<E>::freeNodes = 0;
	template <typename E> int Link<E>::activeNodes = 0;

	template <typename E>
	Link<E>* Link<E> :: freelist = NULL;

template <typename E> class LinkedList: public Link<E> {
	Link<E>* head; // Pointer to list header
	Link<E>* tail; // Pointer to last element
	Link<E>* curr; // Access to current element
	void init() { // Intialization helper method
		curr = tail = head = new Link<E>;


LinkedList() {init();} // Constructor
~LinkedList() {} //Destructor

void clear() { // Return link nodes to free store
	while(head != NULL) {
    Link<E>* temp = head;
	head = head->next;
	delete temp;

Open in new window


Finally, I want the driver.cpp to be able to get a linkedlist from the LinkedList class. I assume I need to make another .h file since I don't want to modify the existing one.  Is that right, and if so, what does it have to look like?
Since tis is a templated linked list, the answer to your initial question is a clear 'yes'. You can use any type you want, no matter whether it is declared in the header file. The compiler will incorporate that type into the template class and you are on teh safe side. Think of it as if the compiler would internally substitute the type argument (in your case 'E') with the actual type that you want to add to that linked list. Some further reading: http://en.wikipedia.org/wiki/Template_(C++)
BTW, theat would be like

class foo {
  foo () {};

List<foo> my_foo_list;

Open in new window

if a cpp source wants to instantiate a template class with a type, it needs not only the definition of the template class but also the implementation (as far it was used). otherwise the compiler would not be able to replace the template type with the concrete type.

concretely: if the driver.cpp wants to create a linked list for 'int' type and 'add' items by using the 'append' member function, the driver.cpp needs to include both the LinkedList class and Link definition and - at least - the implementation of the LinkedList<E>::append function.

because of that, normally a template class header contains both the definition and the implementation of the template class and you would not have a LinkList.cpp file (nor a Link.cpp file).

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.