可能重複:
Why can templates only be implemented in the header file?麻煩與單鏈表實現模板
我有一個單向鏈表按字母順序插入新的書名,並刪除它們。我現在試圖將其轉換爲模板程序,以便可以使用除Book之外的其他對象。我已經經歷了所有錯誤的工作,但在構建具有以下我仍然失敗:
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
我不太知道我做錯了所以這裏是代碼:
//--------------------------------------------------------------
// 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;
}
/* /當成功運行,這應該是輸出:
後插入:
A標題
d標題
Ë標題
˚F標題
克標題
ħ標題
後刪除:
d標題
˚F標題
第二插入件後:
A標題
d標題
Ë標題
˚F標題
G標題
當它不使用模板^ h標題
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];
};
這個程序工作得很好。我沒有包含ObjectList.cpp的代碼,因爲它大約有160行,並不認爲包含它是完全必要的。讓我知道你是否需要看到它。
任何數量的幫助表示讚賞這個最有可能的菜鳥錯誤。
硬件信息: 2011 15" MacBook Pro的運行OS X Lion的 NetBeans IDE的所有更新
模板ObjectList類的所有方法的實現必須在頭文件(或者當你使用'ObjectList'類時至少包含某種方式) –
Mat
有什麼理由不使用'std :: list <>'? –
我打算建議一個std :: list,但是這個行爲更像是一個std :: set,因爲這些書籍是按照字母順序存儲的。 – Joel