arrays - Classic Linker Mach error in C++ -
i don't know c++ , i'm teaching myself adt's , data structures. unfortunately every time need compile code classic xcode error: mach link error. tend understand these errors when they're in objective-c (because means i'm missing file). made sure add file target avoid issue, didn't seem work. neither did classic turn xcode on , off trick.
below error , code:
undefined symbols architecture x86_64: "arraybag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::getindexof(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from: arraybag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::remove(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.o "arraybag<int>::getindexof(int const&) const", referenced from: arraybag<int>::remove(int const&) in main.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation) #ifndef _array_bag #define _array_bag #include "baginterface.h" #include <vector> template<class itemtype> class arraybag : public baginterface<itemtype> { private: static const int default_capacity = 24; // bag size bit bigger allow // adding bags. itemtype items[default_capacity]; // array of bag items int itemcount; // current count of bag items int maxitems; // max capacity of bag // returns either index of element in array items // contains given target or -1, if array not contain // target. int getindexof(const itemtype& target) const; public: arraybag(); int getcurrentsize() const; bool isempty() const; bool add(const itemtype& newentry); void clear(); bool remove(const itemtype& anentry); bool contains(const itemtype& target) const; int getfrequencyof(const itemtype& anentry) const; arraybag<string> merge(arraybag<string> a, arraybag<string> b); std::vector<itemtype> tovector() const; #include "arraybag.h" #include <cstddef> template<class itemtype> arraybag<itemtype>::arraybag(): itemcount(0), maxitems(default_capacity) { } // end default constructor template<class itemtype> int arraybag<itemtype>::getcurrentsize() const { return itemcount; } // end getcurrentsize template<class itemtype> bool arraybag<itemtype>::isempty() const { return itemcount == 0; } // end isempty template<class itemtype> bool arraybag<itemtype>::add(const itemtype& newentry) { bool hasroomtoadd = (itemcount < maxitems); if (hasroomtoadd) { items[itemcount] = newentry; itemcount++; } // end if return hasroomtoadd; } // end add template<class itemtype> void arraybag<itemtype>::clear() { itemcount = 0; } // end clear template<class itemtype> bool arraybag<itemtype>::remove(const itemtype& anentry) { int locatedindex = getindexof(anentry); bool canremoveitem = !isempty() && (locatedindex > -1); if (canremoveitem) { itemcount--; items[locatedindex]=items[itemcount]; }//end if return canremoveitem; } //end remove template<class itemtype> bool arraybag<itemtype>::contains(const itemtype& anentry) const { bool found = false; int curindex = 0; //current array index while(!found && (curindex < itemcount)) { if(anentry == items[curindex]) { found = true; } //end if curindex++; //increment next entry } //end while return found; } template<class itemtype> int arraybag<itemtype>::getfrequencyof(const itemtype& anentry) const { int frequency = 0; int curindex = 0; //current index array while(curindex < itemcount) { if (items[curindex] == anentry) { frequency++; } //end if curindex++; //incremenet next entry } //end while return frequency; } //end getfrequencyof template<class itemtype> vector<itemtype> arraybag<itemtype>::tovector() const { vector<itemtype> bagcontents; (int = 0; < itemcount; i++) bagcontents.push_back(items[i]); return bagcontents; } // end tovector #include <iostream> #include <string> #include "arraybag.h" #include "arraybag.cpp" using namespace std; void displaybag(arraybag<string>& bag) { cout << "the bag contains " << bag.getcurrentsize() << " items:" << endl; vector<string> bagitems = bag.tovector(); int numberofentries = (int)bagitems.size(); (int = 0; < numberofentries; i++) { cout << bagitems[i] << " "; } // end cout << endl << endl; } // end displaybag void displaybag(arraybag<int>& bag) { cout << "the bag contains " << bag.getcurrentsize() << " items:" << endl; vector<int> bagitems = bag.tovector(); int numberofentries = (int)bagitems.size(); (int = 0; < numberofentries; i++) { cout << bagitems[i] << " "; } // end cout << endl << endl; } // end displaybag int sumofbag(arraybag<int>& abag) { int sum=0; vector<int> abagvector = abag.tovector(); int numberofentries = (int) abagvector.size(); (int = 0; < numberofentries; i++) { sum += abagvector[i]; } //cout << "the sum of bag " << sum << endl; return sum; } void bagtester(arraybag<string>& bag) { cout << "isempty: returns " << bag.isempty() << "; should 1 (true)" << endl; displaybag(bag); string items[] = {"one", "two", "three", "four", "five", "one"}; cout << "add 6 items bag: " << endl; (int = 0; < 6; i++) { bag.add(items[i]); } // end displaybag(bag); cout << "isempty: returns " << bag.isempty() << "; should 0 (false)" << endl; cout << "getcurrentsize: returns " << bag.getcurrentsize() << "; should 6" << endl; cout << "try add entry: add(\"extra\") returns " << bag.add("extra") << endl; displaybag(bag); } // end bagtester int main() { arraybag<string> bag; cout << "testing array-based bag:" << endl; cout << "the initial bag empty." << endl; bagtester(bag); arraybag<string> bag_of_strings1; string items[] = {"tom","dick","harry"}; (int i=0; i<3; i++) {bag_of_strings1.add(items[i]);} displaybag(bag_of_strings1); arraybag<string> bag_of_strings2; string new_items[] = {"now","is","the","time","for","all"}; for(int i=0; i<6; i++){ bag_of_strings2.add(new_items[i]); } displaybag(bag_of_strings2); //arraybag<string> newbag=merge(bag_of_strings1,bag_of_strings2); //displaybag(newbag); arraybag<int> bag_of_ints; int array_of_ints[]={6,7,85,9,12,15}; (int i=0;i<6;i++){ bag_of_ints.add(array_of_ints[i]); } displaybag(bag_of_ints); cout<<sumofbag(bag_of_ints)<<endl; return 0; } // end main
the linker telling there no implementation arraybag::getindexof. implementation missing code dump. try adding this:
int arraybag<itemtype>::getindexof(const itemtype& target) const { // todo return 0; }
unlike normal classes, template classes should defined in single header file without corresponding .cpp file.
Comments
Post a Comment