有人可以告诉我为什么会出现此错误-模板LinkedList

时间:2020-01-29 19:37:04

标签: c++ c++11 templates gcc doubly-linked-list

我一直在尝试用c ++中的模板实现双向链接列表。我遇到一些问题,说没有引用我正在调用的特定类型的成员函数,因此我在.cpp文件的末尾添加了一个辅助函数,以告诉编译器替换模板的模板。成员函数针对每种感兴趣的类型实现它们的功能,但是,我仍然收到以下错误,部分表明该函数未实现。

错误:

/usr/bin/ld: /tmp/ccDPJJS9.o: in function `__static_initialization_and_destruction_0(int, int)':
linkedlist.cpp:(.text+0xb1): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: linkedlist.cpp:(.text+0xc6): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: /tmp/ccDPJJS9.o: in function `LinkedList<int>::LinkedList()':
linkedlist.cpp:(.text._ZN10LinkedListIiEC2Ev[_ZN10LinkedListIiEC5Ev]+0x19): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: linkedlist.cpp:(.text._ZN10LinkedListIiEC2Ev[_ZN10LinkedListIiEC5Ev]+0x55): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/ccDPJJS9.o: in function `LinkedList<int>::~LinkedList()':
linkedlist.cpp:(.text._ZN10LinkedListIiED2Ev[_ZN10LinkedListIiED5Ev]+0x2f): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: linkedlist.cpp:(.text._ZN10LinkedListIiED2Ev[_ZN10LinkedListIiED5Ev]+0x50): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/ccDPJJS9.o: in function `LinkedList<float>::LinkedList()':
linkedlist.cpp:(.text._ZN10LinkedListIfEC2Ev[_ZN10LinkedListIfEC5Ev]+0x19): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: linkedlist.cpp:(.text._ZN10LinkedListIfEC2Ev[_ZN10LinkedListIfEC5Ev]+0x55): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/ccDPJJS9.o: in function `LinkedList<float>::~LinkedList()':
linkedlist.cpp:(.text._ZN10LinkedListIfED2Ev[_ZN10LinkedListIfED5Ev]+0x2f): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: linkedlist.cpp:(.text._ZN10LinkedListIfED2Ev[_ZN10LinkedListIfED5Ev]+0x50): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/ccDPJJS9.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
/usr/bin/ld: /tmp/cc0Da8yc.o: in function `main':
main.cpp:(.text+0x15): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: main.cpp:(.text+0x25): undefined reference to `LinkedList<int>::LinkedList(int)'
/usr/bin/ld: main.cpp:(.text+0x3a): undefined reference to `LinkedList<int>::push_back(int)'
/usr/bin/ld: main.cpp:(.text+0x4b): undefined reference to `LinkedList<int>::push_back(int)'
/usr/bin/ld: main.cpp:(.text+0x5c): undefined reference to `LinkedList<int>::push_back(int)'
/usr/bin/ld: main.cpp:(.text+0x6d): undefined reference to `LinkedList<int>::push_back(int)'
/usr/bin/ld: main.cpp:(.text+0x79): undefined reference to `LinkedList<int>::traverse()'
/usr/bin/ld: main.cpp:(.text+0x94): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/cc0Da8yc.o: in function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0xd5): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.cpp:(.text+0xea): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: /tmp/ccN0tGib.o: in function `Node<int>::~Node()':
node.cpp:(.text._ZN4NodeIiED2Ev[_ZN4NodeIiED5Ev]+0x2f): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: node.cpp:(.text._ZN4NodeIiED2Ev[_ZN4NodeIiED5Ev]+0x51): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/ccN0tGib.o: in function `Node<float>::~Node()':
node.cpp:(.text._ZN4NodeIfED2Ev[_ZN4NodeIfED5Ev]+0x2f): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: node.cpp:(.text._ZN4NodeIfED2Ev[_ZN4NodeIfED5Ev]+0x51): undefined reference to `operator delete(void*, unsigned long)'
collect2: error: ld returned 1 exit status

node.h:

#ifndef _NODE_H
#define _NODE_H

template <typename T> class LinkedList;
template <typename T>
class Node
{
   friend class LinkedList<T>;

private:
   T data;
   Node<T> *previous;
   Node<T> *next;

public:
   Node();
   Node(T data);
   void operator=(const Node<T> &rhs);
   ~Node();
};

#endif //_NODE_H

node.cpp:

#include "node.h"

template <typename T>
Node<T>::Node()
   : data{}, previous{nullptr}, next{nullptr} {}

template <typename T>
Node<T>::Node(T data)
   : data(data), previous{nullptr}, next{nullptr} {}

template <typename T>
Node<T>::~Node() {
   delete previous;
   delete next;
}

template <typename T>
void Node<T>::operator=(const Node<T> &rhs) {
   // body
}

void node_classes() {
   Node<int> int_node;
   Node<float> float_node;
}

linkedlist.h:

#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
#include "node.h"

template <typename T>
class LinkedList
{
private:
   Node<T> *head;
   Node<T> *tail;
   void insert_in_place(Node<T> *current, T data);

public:
   LinkedList();
   LinkedList(T data);
   class iterator
   {
      Node<T> *current;
   public:
      iterator(Node<T> *node_ptr);
      T operator*();
      iterator &operator++();
      iterator &operator--();
      ~iterator();
   };
   void push_back(T data);
   void push_front(T data);
   void insert(int index, T data);
   void traverse();
   LinkedList<T> *begin();
   LinkedList<T> *end();
   ~LinkedList();
};

#endif // _LINKEDLIST_H

linkedlist.cpp:

#include <stdexcept>
#include <iostream>
#include "linkedlist.h"

template <typename T>
LinkedList<T>::LinkedList() {
   Node<T> *node = new Node<T>();
   head = node;
   tail = node;
}

template <typename T>
LinkedList<T>::LinkedList(T data) {
   // body
}

template <typename T>
void LinkedList<T>::push_back(T data) {
   // body
}

template <typename T>
void LinkedList<T>::push_front(T data) {
   // body
}

template <typename T>
LinkedList<T> *LinkedList<T>::begin() {
   // body
}

template <typename T>
LinkedList<T> *LinkedList<T>::end() {
   // body
}

template <typename T>
void LinkedList<T>::insert_in_place(Node<T> *current, T data) {
   // body
}

template <typename T>
void LinkedList<T>::insert(int index, T data) {
   // body
}

template <typename T>
void LinkedList<T>::traverse() {
   // body
}

template <typename T>
LinkedList<T>::~LinkedList() {
   delete tail;
   delete head;
}

// iterator subclass

template <typename T>
LinkedList<T>::iterator::iterator(Node<T> *node_ptr)
   : current(node_ptr) {}

template <typename T>
T LinkedList<T>::iterator::operator*() {
   // body
}

template <typename T>
typename LinkedList<T>::iterator &LinkedList<T>::iterator::operator++() {
   // body
}

template <typename T>
typename LinkedList<T>::iterator &LinkedList<T>::iterator::operator--() {
   // body
}

template <typename T>
LinkedList<T>::iterator::~iterator() {
   delete current;
}

void linkedlist_classes() {
   LinkedList<int> int_linked_list;
   LinkedList<float> float_linked_list;
}

main.cpp:

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

int main() {
   LinkedList<int> *list = new LinkedList<int>(0);

   list->push_back(1);
   list->push_back(2);
   list->push_back(3);
   list->push_back(4);

   list->traverse();

   return 0;
}

在此先感谢您,感谢您为解决和理解问题所提供的帮助。

0 个答案:

没有答案