gpt4 book ai didi

c++ - 在单链表上实现模板的问题

转载 作者:太空宇宙 更新时间:2023-11-04 16:30:01 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com