c++ - 第一次尝试构建通用单链表,报undefined symbols错误

标签 c++ xcode linked-list

正如标题所暗示的,我正在尝试学习数据结构,我从链表开始。我决定将其通用化,因为我认为处理其他数据类型会很有用。

我收到这些相关错误:

enter image description here

我不确定自己做错了什么,也无法识别错误。这是我的代码,从头文件开始:

#ifndef LinkedList_hpp
#define LinkedList_hpp

template<class T>
struct Node {
    T data;
    Node<T>* next;
};

template<class T>
class SingleLinkedList {
private:
    Node<T>* head, tail;
public:
    SingleLinkedList();
    void createNode(const T& theData);
    void display();
    void insert_start(const T& theData);
    void insert_position(int pos, const T& theData);
    void delete_first();
    void delete_last();
    void delete_position(int pos);
    Node<T>* search(Node<T>* head, const T& target);
};

#endif /* LinkedList_hpp */

现在这是关联的 .cpp 文件:

#include "LinkedList.hpp"
#include <iostream>

template<class T>
SingleLinkedList<T>::SingleLinkedList() {
    head = nullptr;
    tail = nullptr;
}

template<class T>
void SingleLinkedList<T>::createNode(const T& theData) {
    Node<T>* temp = new Node<T>;
    temp->data = theData;
    temp->next = nullptr;
    if(head == nullptr) {
        head = temp;
        tail = temp;
        temp = nullptr;
    }
    else {
        tail->next = temp;
        tail = temp;
    }
}

template<class T>
void SingleLinkedList<T>::display() {
    Node<T>* temp = new Node<T>;
    temp = head;
    while(temp != nullptr) {
        std::cout << temp->data << "\t";
        temp = temp->next;
    }
}

template<class T>
void SingleLinkedList<T>::insert_start(const T& theData) {
    Node<T>* temp = new Node<T>;
    temp->data = theData;
    temp->next = head;
    head = temp;
}

template<class T>
void SingleLinkedList<T>::insert_position(int pos, const T &theData) {
    Node<T>* previous = new Node<T>;
    Node<T>* current = new Node<T>;
    Node<T>* temp = new Node<T>;
    temp = head;
    for(int i  = 1; i < pos; i++) {
        previous = current;
        current = current->next;

    }
    temp->data = theData;
    previous->next = temp;
    temp->next = current;
}

template<class T>
void SingleLinkedList<T>::delete_first() {
    Node<T>* temp = new Node<T>;
    temp = head;
    head = head->next;
    delete temp;
}

template<class T>
void SingleLinkedList<T>::delete_last() {
    Node<T>* previous = new Node<T>;
    Node<T>* current = new Node<T>;
    current = head;
    while(current->next != nullptr) {
        previous = current;
        current = current->next;
    }
    tail = previous;
    previous->next = nullptr;
    delete current;
}

template<class T>
void SingleLinkedList<T>::delete_position(int pos) {
    Node<T>* previous = new Node<T>;
    Node<T>* current = new Node<T>;
    current = head;
    for(int i = 1; i < pos; i++) {
        previous = current;
        current = current->next;
    }
    previous->next = current->next;
}

最后是我尝试测试代码的 main.cpp 文件:

#include <iostream>
#include "LinkedList.hpp"


int main(int argc, const char * argv[]) {

    SingleLinkedList<int> obj;
    obj.createNode(2);
    obj.createNode(4);
    obj.createNode(6);
    obj.createNode(8);
    obj.display();



    return 0;
}

最佳答案

您需要将您的成员函数的实现放在头文件中。

关于c++ - 第一次尝试构建通用单链表,报undefined symbols错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50458543/

相关文章:

c++ - 无法将 int 类型转换为时间类型(我的类(class)类型)

xcode - 进度 View xcode的高度

c++ - 从给定的链表在 C++ 中创建反向链表

c - 在链表中存储行 (c)

c++ - boost::interprocess 中 win32 文件锁定的模拟是什么?

c++ - 如何调整布局中的小部件?

c++ - 当鼠标悬停在控件上方时拦截鼠标点击

ios - AudioKit v4.2 上的 AKRhodesPiano 错误(挤压)

ios - cocoapods 的 Xcode 7 链接器错误

c - 无法编译链表程序