Possible Duplicate:
Why can templates only be implemented in the header file?
I have a singly linked list that inserts new book titles alphabetically and also deletes them. I am now trying to convert this to a template program so that other object other than Book can be utilized. I have worked through all errors but am still failing at build with the following:
Undefined symbols for architecture x86_64:
"ObjectList<Book>::insert(Book*)", referenced from:
_main in lib.o ,br>
"ObjectList<Book>::getObjectList(char*)", referenced from:
_main in lib.o
"ObjectList<Book>::delet(Book*)", referenced from:
_main in lib.o
I'm not quite sure what I have done wrong so here is the code:
//--------------------------------------------------------------
// lib.cpp
//
//--------------------------------------------------------------
#include <iostream>
using namespace std;
#include "ObjectList.h"
#include "Book.h"
int main(int argc, char* argv[]) {
//--------------------------------------------------------------
// Creates a BookList object, adds several books to the list,
// then prints them.
//--------------------------------------------------------------
char list[2048];
ObjectList<Book> *books = new ObjectList<Book>();
books->insert (new Book("F Title"));
books->insert (new Book("D Title"));
books->insert (new Book("G Title"));
books->insert (new Book("A Title"));
books->insert (new Book("E Title"));
books->insert (new Book("H Title"));
cout << "After inserts:\n";
cout << books->getObjectList(list) << endl;;
//*/
books->delet (new Book("A Title"));
books->delet (new Book("H Title"));
books->delet (new Book("G Title"));
books->delet (new Book("E Title"));
cout << "After deletes:\n";
cout << books->getObjectList(list) << endl;;
books->insert (new Book("A Title"));
books->insert (new Book("E Title"));
books->insert (new Book("H Title"));
books->insert (new Book("G Title"));
cout << "After 2nd inserts:\n";
cout << books->getObjectList(list) << endl;;
//*/
return 0;
}
/*/ When running successfully this should be the output:
After inserts:
A Title
D Title
E Title
F Title
G Title
H Title
After deletes:
D Title
F Title
After 2nd inserts:
A Title
D Title
E Title
F Title
G Title
H Title
ObjectList.h
//********************************************************************
// ObjectListt.h
//
// Represents a collection of books.
//*******************************************************************
#include <iostream>
using namespace std;
template<class T>
class ObjectNode {
public:
//--------------------------------------------------------------
// Sets up the node
//--------------------------------------------------------------
ObjectNode() {}
ObjectNode(T *theObject) {
object = theObject;
next = NULL;
}
friend class ObjectList;
private:
T *object;
ObjectNode *next;
};
template<class T>
class ObjectList {
//----------------------------------------------------------------
// Sets up an empty list of books.
//----------------------------------------------------------------
public:
void add(T *);
void insert(T *);
void delet(T *);
char* getObjectList(char *);
ObjectList() {
head = NULL;
}
private:
ObjectNode<T> *head;
};
Book.h
#include <cstring>
#include <iostream>
using namespace std;
//********************************************************************
// Book.h
//
// Represents a single book.
//*******************************************************************
class Book {
public:
Book (char *newTitle) {
strcpy( title, newTitle );
}
int compareTo(Book *test_book)
{
// comparing test_book to this book
int comparison;
comparison = strcmp(test_book->getObject(), title);
return comparison;
}
//*/
char *getObject() {
return title;
}
private:
char title[81];
};
This program worked just fine when it was not using templates. I did not include the code for ObjectList.cpp as it is about 160 lines long and did not think it would be entirely necessary to include. Let me know if you need to see it.
Any amount of help is appreciated for this most likely rookie mistake.
Hardware info: 2011 15" MacBook Pro Running OS X Lion Netbeans IDE with all updates