KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to create a generic function that removes duplicates from an std::vector. Since I don't want to create a function for each vector type, I want to make this a template function that can accept vectors of any type. Here is what I have: //foo.h Class Foo { template<typename T> static void RemoveVectorDuplicates(std::vector<T>& vectorToUpdate); }; //foo.cpp template<typename T> void Foo::RemoveVectorDuplicates(std::vector<T>& vectorToUpdate) { for(typename T::iterator sourceIter = vectorToUpdate.begin(); (sourceIter != vectorToUpdate.end() - 1); sourceIter++) { for(typename T::iterator compareIter = (vectorToUpdate.begin() + 1); compareIter != vectorToUpdate.end(); compareIter++) { if(sourceIter == compareIter) { vectorToUpdate.erase(compareIter); } } } } //SomeOtherClass.cpp #include "foo.h" ... void SomeOtherClass::SomeFunction(void) { std::vector<int> myVector; //fill vector with values Foo::RemoveVectorDuplicates(myVector); } I keep getting a linker error, but it compiles fine. Any ideas as to what I'm doing wrong? UPDATE: Based on the answer given by Iraimbilanja, I went and rewrote the code. However, just in case someone wanted working code to do the RemoveDuplicates function, here it is: //foo.h Class Foo { template<typename T> static void RemoveVectorDuplicates(T& vectorToUpdate){ for(typename T::iterator sourceIter = vectorToUpdate.begin(); sourceIter != vectorToUpdate.end(); sourceIter++) { for(typename T::iterator compareIter = (sourceIter + 1); compareIter != vectorToUpdate.end(); compareIter++) { if(*sourceIter == *compareIter) { compareIter = vectorToUpdate.erase(compareIter); } } } }; Turns out that if I specify std::vector in the signature, the iterator
Tags (comma-separated)
Save Edits
Cancel