C++ 在.h文件中使用带有结构的模板,得到未知错误
C++ Using template with struct in .h file , get unknow error
尝试制作自定义数组列表,第一个错误告诉我将类型Tamplate()添加到结构ListNode中。
但是在遵循错误指令后,反而得到了新的错误。
" 找不到建筑x86_64符号
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <string>
using namespace std;
template<typename T>
struct ListNode<T>{
T value;
ListNode<T>* next;
ListNode(T d,ListNode<T> *n){
value = d;
next = n;
}
};
template<typename T>
class LinkedList
{
public :
LinkedList();
LinkedList(const LinkedList<T>& another);
~LinkedList();
void copy(const LinkedList<T>& another);
void add(T value);
void remove(int index);
int get(int index) const;
bool isEmpty() const;
int size() const;
string toString() const;
void clear();
LinkedList<T>& operator = (const LinkedList<T>& another);
private :
ListNode<T>* x_front;
int x_size;
};
#endif // LINKEDLIST_H
..CPP
#include "linkedlist.h"
#include "strlib.h"
#include <string>
using namespace std;
template<typename T>
LinkedList<T>::LinkedList(){
x_front = NULL;
x_size =0;
}
template<typename T>
LinkedList<T>::~LinkedList(){
clear();
}
template<typename T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList<T>& another){
delete x_front;
copy(another);
return *this;
}
template<typename T>
void LinkedList<T>::copy(const LinkedList<T>& another){
ListNode<T>* anotherCurrent = another.x_front;
while(anotherCurrent!=NULL){
this->add(anotherCurrent->value);
anotherCurrent = anotherCurrent->next;
}
}
template<typename T>
void LinkedList<T>::add(T value){
if(x_front == NULL){
x_front = new ListNode<T>(value,NULL);
}
else {
ListNode<T>*current = x_front;
while(current-> next != NULL){
current = current -> next;
}
current->next = new ListNode<T>(value,NULL);
}
x_size++;
}
template<typename T>
void LinkedList<T>::remove(int index){
if(index==0){
x_front = x_front->next;
}
else {
ListNode<T>* current = x_front;
for(int indexNow = 0 ; indexNow < index -1 ; indexNow++){
current = current->next;
}
current -> next = current -> next -> next;
}
x_size--;
}
template<typename T>
bool LinkedList<T>::isEmpty() const {
if(this->x_size == 0){
return true;
}
else {return false;}
}
template<typename T>
void LinkedList<T>::clear(){
while(this->isEmpty() ==false){
remove(x_size-1);
}
}
template<typename T>
string LinkedList<T>::toString() const{
string mystring;
for(ListNode<T>*p = x_front; p!=NULL ;p = p->next){
mystring = mystring + " " + integerToString(p->value);
}
return mystring;
}
template<typename T>
int LinkedList<T>::size() const{
return (this->x_size);
}
所有模板都必须在头文件中定义
相关文章:
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 未知的 GCC 链接器错误,但已成功构建
- 为什么我得到以下代码地址清理器:未知地址错误的SEGV
- 双链表的擦除值函数,未知错误
- VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- 线程 std::调用未知类型,无法专门化函数错误
- 获取未知分段错误"(地址)在 std::__cxx11::basic_string<char, std::char_traits<char>,..., std::分配器<cha
- Qmake 错误:QT: 图表中的未知模块
- 在 Mac OS x 中制造期间出现错误未知类型名称"constexpr"
- 如何修复QFile打开错误(未知错误),即使文件存在
- 变量错误:未知类型名称
- 如何修复错误:未知的类型名称"namespace"
- gethostbyaddr 错误未知主机
- C++流错误未知
- Opencv 3.0 features2d.hpp错误:未知的AlgorithmInfo
- 项目错误:未知模块(s)在QT: qml快速
- 错误:未知模块(s)在QT: qml快速QT Creator
- QWebView的Android目标Qt5.3在windows pc上,错误:未知模块(s)在QT: webkitwid
- STL向量错误:未知类型名称