list - Error in C++ : redefinition of class constructor using templates -
anybody know how can fix these errors? have been looking @ while , cannot figure out do.
error:
indexlist.cpp:18: error: redefinition of `indexlist<t>::indexlist()' indexlist.cpp:18: error: `indexlist<t>::indexlist()' declared here indexlist.cpp:30: error: redefinition of `bool indexlist<t>::append(t)' indexlist.cpp:30: error: `bool indexlist<t>::append(t)' declared here cpp file:
// // // // // // #include "indexlist.h" #include <iostream> using namespace std; //constuctor //descriptions: initializes numberofelement 0 // initializes maxsize 100 //parameters: none //return: none template <class t> indexlist<t>::indexlist() { numberofelements = 0; maxsize = 100; } //name: append //purpose: adds element end of list. if array full, returns false //paramters: value - thing append //return: true if append succeeds, false otherwise template <class t> bool indexlist<t>::append(t value) { if (maxsize > numberofelements) { list[numberofelements] = value; numberofelements ++; return true; } else return false; } i didn't put the cpp file because rest of errors similar ones above, , quite long
header:
#include <iostream> using namespace std; #ifndef indexlist_h #define indexlist_h template <class t> class indexlist { public: indexlist(); bool append(t value); bool insert(int indx, t value); bool replace(int indx, t newvalue); bool retrieve(int indx, t &value) const; bool remove(int indx); void sort(); int search(t value) const; private: t list[100]; int numberofelements; int maxsize; }; template <class t> ostream &operator<<(ostream &outstream, const indexlist<t> &lst); #include "indexlist.cpp" #endif i did put entire header
each of 2 files tries include other, can cause preprocessor output repeated code.
take #include "indexlist.h" out of file indexlist.cpp.
also, build process should not attempt compile indexlist.cpp object file.
another way arrange things put contents have in indexlist.cpp near end of indexlist.h, , there no indexlist.cpp file @ all.
Comments
Post a Comment