因此,我已经搜索过Google,Binged,并且尝试了许多不同的潜在修复程序,但仍然无法弄清为什么我的链接列表实现无法编译。我知道这与我的班级名称有关,但是就我所知。我最初将标头拆分为不同的子文件夹,但现在我将其全部放在同一文件夹中只是为了编译内容。我承认,自从本科毕业以来,我在C ++中没有做太多事情,但这就是为什么我试图清除这些蜘蛛网的一部分。任何建议将不胜感激。我附上下面的代码。这是我当前的gcc编译命令:
g++ main.cpp list.cpp -o llist
list.h
#ifndef TEO_LIST_H
#define TEO_LIST_H
#include <iostream>
template<typename T>
struct Node {
public:
T data;
Node<T>* next;
Node<T>* prev;
};
template<class T>
class List {
private:
int length;
Node<T>* head;
Node<T>* tail;
Node<T>* curr;
public:
// Constructors
List();
int Length();
// Node Access
Node<T>* NextNode();
Node<T>* PrevNode();
// Node Actions
void InsertAfter(Node<T>* ptr);
void InsertBefore(Node<T>* ptr);
Node<T>* DeleteAfter();
Node<T>* DeleteBefore();
void StepNext();
void StepPrev();
void StepHead();
void StepTail();
// Expose List Data
bool IsHead();
bool IsTail();
T Data();
};
#endif
list.cpp
#include "list.h"
template<class T>
List<T>::List() {
length = 0;
head = NULL;
tail = NULL;
curr = NULL;
}
template<class T>
int List<T>::Length() {
return this.length;
}
template<class T>
Node<T>* List<T>::NextNode() {
return this.curr->next;
}
template<class T>
Node<T>* List<T>::PrevNode() {
return this.curr->prev;
}
/* This method inserts a node after the current position in the list. */
template<class T>
void List<T>::InsertAfter(Node<T>* ptr) {
if (this.length = 0) {
// Insert first node in the list
this.head = ptr;
this.tail = ptr;
this.curr = ptr;
}
else {
// Insert a new node in an existing list
// Set the new links moving forward (curr -> ptr -> next)
ptr->next = this.curr->next;
this.curr->next = ptr;
// Set the new links moving backward (next -> ptr -> curr)
if (ptr->next != NULL) // ptr = tail
ptr->next->prev = ptr;
ptr->prev = this.curr;
}
this.length++;
}
/* This method inserts a node before the current position in the list. */
template<class T>
void List<T>::InsertBefore(Node<T>* ptr) {
if (this.length = 0) {
// Insert first node in the list
this.head = ptr;
this.tail = ptr;
this.curr = ptr;
}
else {
// Set the new links moving forward (prev -> ptr -> curr)
ptr->prev = this.curr->prev;
this.curr->prev = ptr;
// Set the new links moving backward (curr -> ptr -> prev)
if (ptr->prev != NULL) // ptr = head
ptr->prev->next = ptr;
ptr->next = this.curr;
}
this.length++;
}
/* This method deletes the next node from the list and returns the deleted node.
If the next node is NULL (aka: curr = tail) do nothing and return NULL. */
template<class T>
Node<T>* List<T>::DeleteAfter() {
// Get the node we are about to delete.
Node<T>* tmpNode = this.curr->next;
if (this.length > 0) // Don't deprecate if in an empty list.
this.length--;
// Per our spec, if tmpNode is NULL, we are at the tail and will return NULL.
if (tmpNode = NULL) return NULL;
// Remove the link to tmpNode moving forward
this.curr->next = tmpNode->next;
// Remove the link to tmpNode moving backward
if (tmpNode->next != NULL) // tmpNode = tail
tmpNode->next->prev = this.curr;
return tmpNode;
}
/* This method deletes the prev node from the list and returns the deleted node.
If the prev node is NULL (aka: curr = head) do nothing and return NULL. */
template<class T>
Node<T>* List<T>::DeleteBefore() {
// Get the node we are about to delete.
Node<T>* tmpNode = this.curr->prev;
if (this.length > 0) // Don't deprecate if in an empty list.
this.length--;
// Per our spec, if tmpNode is NULL, we are at the head and will return NULL.
if (tmpNode = NULL) return NULL;
// Remove the link to tmpNode moving backward
this.curr->prev = tmpNode->prev;
// Remove the link to tmpNode moving forward
if (tmpNode->prev != NULL) // tmpNode = head
tmpNode->prev->next = this.curr;
return tmpNode;
}
/* This method moves forward in the list.
If the next node is NULL, we are at the tail and don't move forward. */
template<class T>
void List<T>::StepNext() {
if (this.curr->next != NULL) this.curr = this.curr->next;
}
/* This method moves forward in the list.
If the prev node is NULL, we are at the head and don't move backward. */
template<class T>
void List<T>::StepPrev() {
if (this.curr->prev != NULL) this.curr = this.curr->prev;
}
/* This method moves to the head of the list. */
template<class T>
void List<T>::StepHead() {
this.curr = this.curr->head;
}
/* This method moves to the tail of the list. */
template<class T>
void List<T>::StepTail() {
this.curr = this.curr->tail;
}
/* This method returns true if curr = head and false otherwise. */
template<class T>
bool List<T>::IsHead() {
if (this.curr->prev = NULL)
return true;
else
return false;
}
/* This method returns true if curr = tail and false otherwise. */
template<class T>
bool List<T>::IsTail() {
if (this.curr->next = NULL)
return true;
else
return false;
}
/* This method returns curr.data. */
template<class T>
T List<T>::Data() {
return this.curr.data;
}
main.cpp
#include <iostream>
#include "list.h"
int main(int argc, char *argv[]) {
List<int> *linkedList = new List<int>();
Node<int> *a, *b, *c;
a->data = 1013;
b->data = 711;
c->data = 1202;
linkedList->InsertAfter(a);
linkedList->InsertBefore(b);
linkedList->StepNext(); // curr = a
linkedList->InsertAfter(c);
linkedList->StepHead();
std::cout << "Traversing forward..." << std::endl;
while (!linkedList->IsTail()) {
std::cout << "Current Node Value: " << linkedList->Data() << std::endl;
linkedList->StepNext();
}
linkedList->StepTail();
std::cout << "Traversing backward..." << std::endl;
while (!linkedList->IsHead()) {
std::cout << "Current Node Value: " << linkedList->Data() << std::endl;
linkedList->StepPrev();
}
return 0;
}
编译错误
/usr/bin/ld: /tmp/ccjGuq0i.o: in function `main':
main.cpp:(.text+0x23): undefined reference to `List<int>::List()'
/usr/bin/ld: main.cpp:(.text+0x58): undefined reference to `List<int>::InsertAfter(Node<int>*)'
/usr/bin/ld: main.cpp:(.text+0x6b): undefined reference to `List<int>::InsertBefore(Node<int>*)'
/usr/bin/ld: main.cpp:(.text+0x77): undefined reference to `List<int>::StepNext()'
/usr/bin/ld: main.cpp:(.text+0x8a): undefined reference to `List<int>::InsertAfter(Node<int>*)'
/usr/bin/ld: main.cpp:(.text+0x96): undefined reference to `List<int>::StepHead()'
/usr/bin/ld: main.cpp:(.text+0xca): undefined reference to `List<int>::IsTail()'
/usr/bin/ld: main.cpp:(.text+0xf3): undefined reference to `List<int>::Data()'
/usr/bin/ld: main.cpp:(.text+0x11e): undefined reference to `List<int>::StepNext()'
/usr/bin/ld: main.cpp:(.text+0x12c): undefined reference to `List<int>::StepTail()'
/usr/bin/ld: main.cpp:(.text+0x160): undefined reference to `List<int>::IsHead()'
/usr/bin/ld: main.cpp:(.text+0x189): undefined reference to `List<int>::Data()'
/usr/bin/ld: main.cpp:(.text+0x1b4): undefined reference to `List<int>::StepPrev()'
collect2: error: ld returned 1 exit status