模板链接列表编译错误
template link list compile error
我正在为学校制定一个计划。它是一个使用模板的链接列表。
我有一个抽象基类:
#pragma once
#include <string>
using namespace std;
template<class T>
class LinkedListInterface
{
public:
LinkedListInterface(void){};
virtual ~LinkedListInterface(void){};
virtual void insertHead(T value) = 0;
virtual void insertTail(T value) = 0;
virtual void insertAfter(T value, T insertionNode) = 0;
virtual void remove(T value) = 0;
virtual void clear() = 0;
virtual T at(int index) = 0;
virtual int size() = 0;
};
我从中派生出一个类:
/* Linklist.h
*
* Created on: Oct 4, 2014
*
*/
#ifndef LINKLIST_H_
#define LINKLIST_H_
#include <iostream>
#include "LinkedListInterface.h"
template<class T>
class Linklist: public LinkedListInterface<T> {
private:
struct _node
{
T val;
Linklist *next;
};
struct _node node ;
Linklist *root = NULL;
Linklist *tail = NULL;
int _size = 0;
int finddup(T value);
public:
Linklist();
virtual ~Linklist();
void insertHead(T value);
void insertTail(T value);
void insertAfter(T value, T insertionNode);
void remove(T value);
void clear();
T at(int index);
int size();
};
#endif /* LINKLIST_H_ */
链接列表.cpp
/*
* Linklist.cpp
*
* Created on: Oct 4, 2014
*
*/
#include "Linklist.h"
template<class T>
Linklist<T>::Linklist()
{
// TODO Auto-generated constructor stub
}
template<class T>
Linklist<T>::~Linklist()
{
// TODO Auto-generated destructor stub
}
template<class T>
int Linklist<T>::finddup(T value)
{
Linklist *tmp = root;
while (tmp)
{
if (tmp->node.val == value)
{
return 1;
}
}
return 0;
}
template<class T>
void Linklist<T>::insertHead(T value)
{
Linklist *tmp = root;
if (finddup(value))
{
return;
}
else
{
if (!root)
{
root = new Linklist<T>();
root->val = value;
root->next = NULL;
tail = root;
_size++;
}
else
{
Linklist *newr = new Linklist<T>();
newr->node.val = value;
newr->node.next = root;
root = &newr;
_size++;
}
}
}
template<class T>
void Linklist<T>::insertTail(T value)
{
Linklist *tmp = root;
if (finddup(value))
{
return;
}
else
{
if (!tail)
{
tail = new Linklist<T>();
tail->val = value;
tail->next = NULL;
root = tail;
_size++;
}
else
{
Linklist *newr = new Linklist<T>();
newr->node.val = value;
newr->node.next = NULL;
tail->node.next = &newr;
tail = &newr;
_size++;
}
}
}
template<class T>
void Linklist<T>::insertAfter(T value, T insertionNode)
{
if (finddup(value))
{
return;
}
else
{
Linklist *tmp = root;
while (tmp)
{
if (tmp->node.val == insertionNode)
{
Linklist *newr = new Linklist<T>();
newr->node.val = value;
newr->node.next = tmp->node.next;
tmp->node.next = &newr;
_size++;
break;
}
else
tmp = tmp->node.next;
}
}
}
template<class T>
void Linklist<T>::remove(T value)
{
Linklist *prev = NULL, *active = NULL;
if (!root)
std::cout << "List is empty" << std::endl;
else
{
if (root && root->node.val == value)
{
Linklist *t = root;
root = t->node.next;
delete t;
_size--;
}
}
prev = root;
active = root->node.next;
while (active)
{
if (active->node.val == value)
{
prev->node.next = active->node.next;
delete active;
_size--;
active = prev->node.next;
}
else
{
prev = active;
active = active->node.next;
}
}
}
template<class T>
void Linklist<T>::clear()
{
Linklist *t = root;
while (t)
{
t = root->node.next;
delete root;
root = t;
}
root = NULL;
tail = NULL;
_size = 0;
}
template<class T>
T Linklist<T>::at(int index)
{
Linklist *t = root;
if (index < _size)
{
for (int i = 0; i < _size; i++)
{
t = t->node.next;
}
}
else
{
t = NULL;
}
return (t);
}
template<class T>
int Linklist<T>::size()
{
return (_size);
}
这些似乎还可以。问题是当我试图在提供并修改的工厂类中创建一个Linklist对象时。
#include "Factory.h"
#include "Linklist.h"
//You may add #include statements here
/*
You will MODIFY THIS DOCUMENT.
*/
/*
getLinkedListInt() and
Creates and returns an object whose class extends LinkedListInterface.
This should be an object of a class you have created.
Example: If you made a class called "LinkedList", you might say, "return new LinkedList<int>();".
*/
LinkedListInterface<int> * Factory::getLinkedListInt()
{
return new Linklist<int>();
}
/*
getLinkedListString() and
Creates and returns an object whose class extends LinkedListInterface.
This should be an object of a class you have created.
Example: If you made a class called "LinkedList", you might say, "return new LinkedList<string>();".
*/
LinkedListInterface<string>* Factory::getLinkedListString()
{
return new Linklist<string>();
}
有人告诉我:
undefined reference to `Linklist<int>::Linklist()'
undefined reference to `Linklist<string>::Linklist()'
当我编译的时候。我不太了解模板,不知道我搞砸了什么。
有人有什么建议吗?
感谢
这是不起作用:
main.cpp
#include "Source.h"
int main() {
Linklist<int> obj;
}
Source.h
#pragma once
template <class T>
class Linklist {
public:
Linklist();
};
来源.cpp
#include "Source.h"
template <class T>
Linklist<T>::Linklist() {}
输出:
错误LNK2019:未解析的外部符号"public:__cdecl Linklist::Linklist(void)"
这是因为模板是如何实例化的。
在您的特定情况下,您可以显式实例化int
和string
类型,也可以更改设计以便在头文件中具有声明和定义。
更多信息:为什么模板只能在头文件中实现?
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?