c++ - 使用模板的链接器错误 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;
}
解决方案
推荐阅读
- blazor - 是否可以在非 .NET 网站中嵌入 Blazor 组件?
- gcc - 是什么导致“x.asm:(.text+0xd): undefined reference to 'y'”?
- excel - 在朋友计算机上加载 VBA 加载项的问题
- c# - SCA 的订阅付款必须启动并运行
- python-3.x - 如何分离子图上的数据?
- c# - 将 Unicode 字符转换回其原始字符串表示形式
- c# - 在调试和发布之间切换后,某些代码和断点不起作用
- tensorflow - 在 YOLO 中标注训练数据
- wordpress - 寻找软件,以便我可以使用基于 GIT 功能分支的临时 URL 启动一个快速测试站点
- sql - 更新 SELECT 查询中的列值