I first implement following functions in the same .cpp file as the main(). Everything works perfectly. But I met some problem when I tried to define them in a header file and implement them in a separate .cpp file.
These are my original code:
template <class T> //implement the sum function of one single container
double sum(const T& source){...}
//explicit specialization for map
template<>
double sum<map<string, double>>(const map<string,double>& source){...}
template <class T> //implement the sum function for two iterators
double sum( const class T::const_iterator& start, const class T::const_iterator& end){...}
//explicit specialization for map (sum function of iterator)
template<>
double sum<map<string, double>>(const map<string, double>::const_iterator& start,const map<string, double>::const_iterator& end){...}
void main(){...}
Then I remove the implementation of the functions and define them in a header file:
template <class T> //implement the sum function of one single container
double sum(const T& source);
//explicit specialization for map
template<>
double sum<map<string, double>>(const map<string,double>& source);
template <class T> //implement the sum function for two iterators
double sum( const class T::const_iterator& start, const class T::const_iterator& end);
//explicit specialization for map (sum function of iterator)
template<>
double sum<map<string, double>>(const map<string, double>::const_iterator& start,const map<string, double>::const_iterator& end);
and implement them in a .cpp file.
Then when I include both of the header file and cpp file, and run the project:
main(){...}
I received error message like:
sum.obj : error LNK2005: "double __cdecl sum<class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >....
Can anyone give a hint what I might have done wrong?
Many thanks!