mercredi 29 janvier 2020

Could some tell me why I'm getting this error - Template LinkedList [duplicate]

I've been trying to implement a doubly-linked list with templates in c++. I've had some problems saying that the member function for the specific type that I was calling wasn't referenced, so I've added at the end of the .cpp files an auxiliary function to tell the compiler to substitute the templates of the member functions for their implementation for each type of interest, though, I'm still getting the following error that says, in part, that the functions wasn't implemented.

The Error:

/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;
}

I thank you in advance and appreciate any help to solve and understand the problem.

Aucun commentaire:

Enregistrer un commentaire