0

Solved
Thanks, I used David Schwartz's answer and solved the problem. Below is the code that I can use.

The original question I have is how to sort a vector of pair, and I get the answer from here :
Sorting a std::vector<std::pair<std::string,bool>> by the string?

Then I want to keep this method in my library my_lib.hpp, so that I can use it when I needed and also, I want to try to make a template for it.
Following is my setting, and my problem is I get this error in eclipse

undefined reference to void haha::pair_sort_second_dec<std::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >(std::vector<std::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&) main.cpp /question line 406 C/C++ Problem

my_lib.hpp

namespace haha{
template <class T>
bool pairCompare_dec(const T& , const T& );
template <class T>
void pair_sort_second_dec(std::vector<T>& );


template <class T>
bool pairCompare_dec(const T& firstElem,const T& secondElem) {
  return firstElem.second > secondElem.second;
}
template <class T>
void pair_sort_second_dec(std::vector<T>& target){
    std::sort(target.begin(),target.end(),pairCompare_dec<T>);
}
};

main.cpp

#include "my_lib.hpp"

int main(int argc,char* argv[]){
    std::vector<std::pair<int,std::string> > test;
    // initial test
    haha::pair_sort_second_dec(test);
    return 0;
}

Anyone knows how to fix it? Thanks in advance.

3
  • "undefined reference to [function-name]" isn't precise text of the error, because there is nothing called [function-name] here. When asking about problems involving an error message, you have to quote the error message exactly. Commented Jan 22, 2014 at 6:38
  • 1
    And the error certainly indicates a location too. Include it and state what part of the shown code it corresponds to. Commented Jan 22, 2014 at 6:39
  • @JanHudec sorry, I have added the error message Commented Jan 22, 2014 at 6:49

1 Answer 1

2
std::sort(target.begin(),target.end(),pairCompare_dec);

Should be:

std::sort(target.begin(),target.end(),pairCompare_dec<T>);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.