在链接列表结构中,c++,templates,linked-list,linker-errors"/>

首页 > 解决方案 > 使用模板的链接器错误 2019在链接列表结构中

问题描述

我正在设计一个非常简单的链接列表,因为我想尝试一些关于内存的东西。我之前创建了链接列表,但这是我第一次使用模板在其他项目中使用它,但我遇到了 2019 年链接错误。

当我在主程序中复制/粘贴类定义和声明时,它按预期工作,但我希望解决我遇到的这个链接问题。我不知道问题出在哪里,所以如果我能得到任何帮助,我将不胜感激。

这是我在头文件中的类声明:

template<class dataType>
class node {
public:
    dataType data;
    node<dataType>* next;
};

template<class dataType>
class nodeInterface {
public:
    node<dataType>* headNode;
    node<dataType>* tailNode;
    node<dataType>* currentNode;
    int nodeCount;

    nodeInterface();
    ~nodeInterface();

    void emptyInterface();

    void addElement(dataType);
    void setHeadNode();
    void setTailNode();

    dataType get();
    dataType unget();
    dataType peek();
    void setCurrent(int);
};

类定义如下:

# include <iostream>
# include "classHeader.h"

template<class dataType>
nodeInterface<dataType>::nodeInterface() {
    headNode = nullptr;
    tailNode = nullptr;
    currentNode = nullptr;

    nodeCount = 0;
}

template<class dataType>
nodeInterface<dataType>::~nodeInterface() {
    emptyInterface();
}

template<class dataType>
void nodeInterface<dataType>::emptyInterface() {
    if (headNode == nullptr) {
        log(0, "Attempting to empty an empty List.");
        return;
    }

    currentNode = headNode;
    node<dataType>* tempPtr = currentNode->next;
    while (tempPtr != nullptr) {
        delete currentNode;
        currentNode = tempPtr;
        tempPtr = currentNode->next;
    }
    delete currentNode;
}

template<class dataType>
void nodeInterface<dataType>::addElement(dataType dataValue) {
    if (headNode == nullptr) {
        headNode = new(std::nothrow) node<dataType>;
        if (headNode == nullptr) {
            log(2, "Couldn't create new node.");
            return;
        }
        headNode->data = dataValue;
        headNode->next = nullptr;

        currentNode = headNode;
        tailNode = headNode;
        nodeCount++;
        return;
    }

    currentNode = tailNode;
    currentNode->next = new(std::nothrow) node<dataType>;
    if (currentNode->next == nullptr) {
        log(2, "Couldn't create new node.");
        return;
    }
    currentNode = currentNode->next;

    currentNode->data = dataValue;
    currentNode->next = nullptr;

    tailNode = currentNode;
    nodeCount++;
    return;
}

template<class dataType>
void nodeInterface<dataType>::setHeadNode() {
    currentNode = headNode;
}

template<class dataType>
void nodeInterface<dataType>::setTailNode() {
    currentNode = tailNode;
}

template<class dataType>
dataType nodeInterface<dataType>::get() {
    if (currentNode == nullptr) {
        log(1, "Attempting to obtain a value of a nullNode.");
        return NULL;
    }

    dataType temp;
    temp = currentNode->data;

    currentNode = currentNode->next;
    return temp;
}

template<class dataType>
dataType nodeInterface<dataType>::unget() { //data structure isn't meant to act as a two dimensional list. This is for the odd case.
    if (currentNode == headNode) {
        log(0, "Attempting to unget at headNode. Returning value of head.");
        return currentNode->data;
    }


    node<dataType>* tempCurrent;

    tempCurrent = currentNode;
    currentNode = headNode;

    while (currentNode.next != tempCurrent)
        currentNode = currentNode->next();

    return currentNode->data;
}

template<class dataType>
dataType nodeInterface<dataType>::peek() {
    if (currentNode == nullptr) {
        log(0, "Attempting to peek from a nullNode.");
        return NULL;
    }

    return currentNode->data;
}

template<class dataType>
void nodeInterface<dataType>::setCurrent(int index) {
    if (index > nodeCount) {
        log(2, "Attempting to move currentNode to nonexisting index.");
        return;
    }

    currentNode = headNode;
    for (int i = 0; i <= index; i++)
        currentNode = currentNode->next;
}

标签: c++templateslinked-listlinker-errors

解决方案


推荐阅读