c++ - undefined reference error, using templates and classes -
this question has answer here:
i practicing coding exam tomorrow, reason cannot program run. keep getting error:
undefined reference ..pract\arraylisttype\main.cpp:47: error: undefined reference 'arraylisttype<std::string>::print() const'
here code have far, see looks pretty alright. tried commenting out lines of code no avail. don't know can wrong.
//header file:
#ifndef arraylisttype_h #define arraylisttype_h using namespace std; template <class elemtype> class arraylisttype { public: const arraylisttype<elemtype>& operator= (const arraylisttype<elemtype>&); //overloads assignment operator bool isempty() const; //determines if list empty bool isfull() const; //determines if list full int listsize() const; //function determine number of elements in list int maxlistsize() const; //function determine size of list void print() const; //function output elements in list bool isitematequal(int location, const elemtype& item) const; //function determine if item entered same item @ specific location void insertat(int location, elemtype& insertitem); //function insert item @ specified location void insertend(const elemtype& insertitem); //function insert item @ end of list void removeat(int location); //function remove item @ specified location void retrieveat(int location, const elemtype& retitem) const; //function retrieve item located @ position specified void replaceat(int location, const elemtype& repitem); //function replace value in list @ specified location void clearlist(); //function remove elements list int seqsearch(const elemtype& item) const; //function search list given item void insert(const elemtype& insertitem); //function insert item list void remove(const elemtype& removeitem); //function remove item list arraylisttype(int size = 100); //constructor initialize size of list 100 arraylisttype(const arraylisttype<elemtype>& otherlist); //copy constructor ~arraylisttype(); //destructor protected: elemtype *list; int length; int maxsize; }; #endif // arraylisttype_h
//definition file
#include <iostream> #include <string> #include "arraylisttype.h" using namespace std; template <class elemtype> bool arraylisttype<elemtype>::isempty() const { return (length == 0); } template <class elemtype> bool arraylisttype<elemtype>::isfull() const { return (length == maxsize); } template <class elemtype> int arraylisttype<elemtype>::listsize() const { return length; } template <class elemtype> int arraylisttype<elemtype>::maxlistsize() const { return maxsize; } //all above functions have running time of o(1) - constant //produce same result int eh same time due having 1 line run. //_____________________________________________________________________________ template <class elemtype> // o(n) void arraylisttype<elemtype>::print() const { (int = 0; <length; i++) { cout << list[i] << " "; } cout << endl; } //the print function have running time of o(n) linear size of list //and how full list is. template <class elemtype> // o(1) - constant bool arraylisttype<elemtype>::isitematequal(int location, const elemtype &item) const { return(list[location] == item); } template <class elemtype> //o(n) void arraylisttype<elemtype>::insertat(int location, elemtype &insertitem) { if (location < 0 || location >= maxsize) cerr << "the position inserted @ out of range!"; else if (length >= maxsize) cerr << "cannot insert in full list"; else { (int = length; > location; i--) list[i] = list[i - 1]; // move elements down. list[location] = insertitem; // insert item @ specified location length++; } } template <class elemtype> void arraylisttype<elemtype>::insertend(const elemtype &insertitem) { if (length >= maxsize) cerr << "cannot insert in full list"; else { list[length] = insertitem; length++; } } template <class elemtype> void arraylisttype<elemtype>::removeat(int location) { if (location < 0 || location >= maxsize) cerr << "the location fo item retrieved out of bounds"; else { (int = location; < length - 1; i++) list[i] = list[i+1]; length--; } } template <class elemtype> void arraylisttype<elemtype>::retrieveat(int location, const elemtype& retitem) const { if (location < 0 || location >= maxsize) cerr << "the location fo item retrieved out of bounds"; else (retitem = list[location]); } template <class elemtype> void arraylisttype<elemtype>::replaceat(int location, const elemtype &repitem) { if (location < 0 || location >= length) cerr << "the location of item replaced out of bounds."; else list[location] = repitem; } template <class elemtype> void arraylisttype<elemtype>::clearlist() { length = 0; } template <class elemtype> arraylisttype<elemtype>::arraylisttype(int size) { if (size < 0) { cerr << "the array size must positive. creating array of size 100"; maxsize = 100; } else maxsize = size; length = 0; list = new elemtype[maxsize]; assert(list!=null); } template <class elemtype> arraylisttype<elemtype>::~arraylisttype() { delete [] list; } template <class elemtype> arraylisttype<elemtype>::arraylisttype(const arraylisttype<elemtype> &otherlist) { maxsize = otherlist.maxsize; length = otherlist.length; list = new elemtype[maxsize]; (int j = 0; j < length; j++) list[j] = otherlist.list[j]; } template <class elemtype> const arraylisttype<elemtype>& arraylisttype<elemtype>::operator=(const arraylisttype<elemtype>& otherlist) { if (this != &otherlist) { delete [] list; maxsize = otherlist.maxsize; length = otherlist.length; list = new elemtype[maxsize]; assert(list != null); (int = 0; < length; i++) list[i] = otherlist.list[i]; } return *this; } template <class elemtype> int arraylisttype<elemtype>::seqsearch(const elemtype &item) const { int loc; bool found = false; (loc = 0; loc < length; loc++) if (list[loc] == item) { found = true; break; } if (found) return loc; else return -1; } template <class elemtype> void arraylisttype<elemtype>::insert(const elemtype &insertitem) { int loc; if (length == 0) list[length++] == insertitem; else if (length == maxsize) cerr << "cannot insert full list."; else { loc == seqsearch(insertitem); if (loc == -1) list[length++] = insertitem; else cerr << "the item inserted in list." << "\nno duplicates allowed."; } } template <class elemtype> void arraylisttype<elemtype>::remove(const elemtype &removeitem) { int loc; if (length == 0) cerr << "cannot delete empty list"; else { loc = seqsearch(removeitem); if (loc != -1) removeitem(loc); else cout << "the item deleted not int eh list."; } }
//main
#include <iostream> #include <string> #include "arraylisttype.h" using namespace std; int main() { arraylisttype<int> intlist(100); arraylisttype<string> stringlist; int number; cout << "list 10: enter 5 integers: "; (int counter = 0; counter < 5; counter++) { cin >> number; intlist.insertat(counter, number); } cout << endl; cout << "list 19: list entered is: "; intlist.print(); cout << endl; cout << "line 20: enter item deleted: "; cin >> number; intlist.remove(number); cout << "line 23: after removing " << number << ", lsit is: " << endl; intlist.print(); cout << endl; string str; cout << "line 27: enter 5 strings: "; (int counter = 0; counter < 6; counter++) { cin >> str; stringlist.insertat(counter, str); } cout << endl; cout << "line 34: list entered is: "; stringlist.print(); cout << endl; cout << "line 37: enter string deleted: "; cin >> str; stringlist.remove(str); cout << "line 40: after removing " << str << " list is: " << endl; stringlist.print(); return 0; }
when use templates classes or methods, compiler generates code , compiler needs access definition. therefor have put template method definition header files , include use them. templates compile time features , not run-time. template definitions can not isolated cpp files.
Comments
Post a Comment