error LNK2001 and error LNK1120 when trying to compile and build the program

When I was trying to compile and build my program by MS VC++6, it gave me the following error:
-------------------------------------------
Linking...
Sort.obj : error LNK2001: unresolved external symbol "int __cdecl cmpNum(int,int)" (?cmpNum@@YAHHH@Z)
Debug/Sort.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Sort.exe - 2 error(s), 0 warning(s)
-----------------------------------------------

I can see it has something to do with the function "cmpNum(int, int), but I don't know what's going wrong and how to resolve it, though everything seems fine for me. The following is some code of my program relating the function "cmpNum(int, int)" which is a template function and I need to pass it as a parameter into the "Sort" fuction. Any help will be really appreciated!!

template <class T>
int cmpNum(T t1, T t2);

template <class T, class S>
void Sort(T *s, T *e, S cmp);

int main()
{
      int nArray[4] = {2, 1, 0, 5};
      Sort(nArray, nArray + 4, cmpNum<int>);
      char *pcArray[6] = {"abc", "abcde", "a", "ab", "abcd", "abcdef"};
      Sort(pcArray, pcArray + 5, strcmp);
      return 0;
}
caibeierAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Where is implementation of cmpNum function?
jweston1Commented:
How about a templated class with a typedef for the cmp function. Something like,

template <class T>
class S
{
public:
      typedef int (*CMP)(T t1, T t2);
};

template <class T>
int cmp(T t1, T t2)
{
      return t1 == t2;
}

template <class T>
int Sort(T *s, T *e, S<T>::CMP pfn)
{
      return pfn(*s, *e);
}

int main()
{
      int nArray[4] = {2, 1, 0, 5};
      S<int>::CMP pfnInt = cmp<int>;
      Sort(nArray, nArray + 1, pfnInt);
      CString cstrArray[2] = {"awd", "dwg"};
      S<CString>::CMP pfnCString = cmp<CString>;
      Sort(cstrArray, cstrArray + 1, pfnCString);
      return 0;
}
Jaime OlivaresSoftware ArchitectCommented:
Maybe you have not declared the .cpp file that contains cmpNum() function as a project file.
Then, your project is compiling properly because it founds .h file, but linking bad because the .cpp is not considered.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

AlexFMCommented:
Template function implementation must be in the same cpp file or in h file included to it. Templated functions and classes are not subject of linking because they must be handled by compiler.
itsmeandnobodyelseCommented:
You may try to use a temporary variable for the function pointer cause VC6 isn't full compliant to c++ standard, i. e. the template function doesn't get an instantiation when used as an argument only.

int main()
{
     int nArray[4] = {2, 1, 0, 5};

     int (*intCompFunc)(int, int)  = cmpNum<int>;
     Sort(nArray, nArray + 4, intCompFunc);
     const char *pcArray[6] = {"abc", "abcde", "a", "ab", "abcd", "abcdef"};
     Sort(pcArray, pcArray + 5, strcmp);
     return 0;
}

Regards, Alex

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
caibeierAuthor Commented:
Thank you all! It worked! I really appreciate you times!! (O:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.