c++ - template link list compile error -
c++ - template link list compile error -
this question has reply here:
why can templates implemented in header file? 9 answersi working on programme school. link list using templates.
i have abstract base of operations class:
#pragma 1 time #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; };
and class derived it:
/* 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_ */
linklist.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) { homecoming 1; } } homecoming 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 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) { (int = 0; < _size; i++) { t = t->node.next; } } else { t = null; } homecoming (t); } template<class t> int linklist<t>::size() { homecoming (_size); }
those seem ok. problem when seek create linklist object in mill class provided , modified.
#include "factory.h" #include "linklist.h" //you may add together #include statements here /* modify document. */ /* getlinkedlistint() , creates , returns object class extends linkedlistinterface. should object of class have created. example: if made class called "linkedlist", might say, "return new linkedlist<int>();". */ linkedlistinterface<int> * factory::getlinkedlistint() { homecoming new linklist<int>(); } /* getlinkedliststring() , creates , returns object class extends linkedlistinterface. should object of class have created. example: if made class called "linkedlist", might say, "return new linkedlist<string>();". */ linkedlistinterface<string>* factory::getlinkedliststring() { homecoming new linklist<string>(); }
i told:
undefined reference `linklist<int>::linklist()' undefined reference `linklist<string>::linklist()'
when compile. don't understand templates plenty know messed on.
does have advice?
thanks
this not going work:
main.cpp
#include "source.h" int main() { linklist<int> obj; }
source.h
#pragma 1 time template <class t> class linklist { public: linklist(); };
source.cpp
#include "source.h" template <class t> linklist<t>::linklist() {}
output:
error lnk2019: unresolved external symbol "public: __cdecl linklist::linklist(void)"
this happens because of how templates instantiated.
in specific case can either explicitly instantiate int
, string
types or can alter design in order have declaration , definition in header file.
for more info: why can templates implemented in header file?
c++ class templates
Comments
Post a Comment