- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正如标题所暗示的,我正在尝试学习数据结构,我从链表开始。我决定将其通用化,因为我认为处理其他数据类型会很有用。
我收到这些相关错误:
我不确定自己做错了什么,也无法识别错误。这是我的代码,从头文件开始:
#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/
我是 Java 新手,我遇到了这个我无法解决的问题。我继承了这个项目,并且我的 scriptlet 之一中有以下代码: DefaultLogger.logMessage("DEBUG path: "+
在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况(报selenium.common.exceptions.NoSuchElementException),一般
我之前已经这样做过,但令我惊讶的是 CListCtrl 不会以颜色显示文本。我在对话框上有 ListView 控件。我正在使用 VS2010,是否还缺少其他东西? void CGameView::On
我正在尝试计算 4 个表中每个学生的 GPA: Student(STUDENT_ID, STUDENT_LNAME, STUDENT_FNAME, MAJOR) Course(COURSE_NO, C
当我在 windows 和 mac 上使用 jasper 报告时它工作正常,当我将我的应用程序部署到 linux 机器 并尝试生成它抛出的报告时 net.sf.jasperreports.engine
我正在构建 iOS 应用并尝试实现应用内购买(非消费品)。 所有 bundle ID 等都已设置并正常工作,当我获取 Apple Store 服务器时,我可以看到我的产品有效。但是,在测试时,我在 p
我正在尝试使用非固定字符数组读取用户输入,但当我在键盘上输入内容时它只是软崩溃(没有崩溃窗口)。当我在在线 C 编译器上运行它时,它说 Segmentation fault (core dumped)
事实: 无根 podman 非常适合 uid 1480 无根 podman 为 uid 2088 失败 中央操作系统 7 内核 3.10.0-1062.1.2.el7.x86_64 podman 版本
根据 homebrew-brew 官方的解释得知,MongoDB 不再是开源的了,并且已经从 Homebrew中移除 #43770 正是由于 MongoDB 的商业化不太理想,所以它选择了闭源。所
我用命令禁用collectstatic heroku config:set DISABLE_COLLECTSTATIC=1 成功将我的项目推送到 Heroku 后,手动 collectstatic 如
代码如下: public class TryStuffOutHere { public static void main(String[] args) {
我已经设置了我的 redis 服务器,以便 CONFIG GET dir --> "/var/lib/redis" 和 CONFIG GET dbfilename --> "redis.rdb". 但
我是一名优秀的程序员,十分优秀!