【发布时间】:2011-11-15 20:26:58
【问题描述】:
可能重复:
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;
}
/*/ 运行成功时应该是这样的输出:
插入后:
标题
D 标题
E 标题
F 标题
G 标题
H 标题
删除后:
D 标题
F 标题
第二次插入后:
标题
D 标题
E 标题
F 标题
G 标题
H 标题
对象列表.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;
};
书.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<Book>类的时候以某种方式包含) -
有什么理由不使用
std::list<>? -
我也打算推荐一个 std::list 但这更像是一个 std::set 因为书籍是按字母顺序存储的。
-
@DavidThornley:必须是作业或练习。
-
糟糕的 C++ 代码。看起来您正在尝试使用 C++ 编写 Java。
标签: c++ list templates linked-list