0

I get a link error in following situation, don't find where the defect is:

qutil.h

template <class T>
bool isPrime( T number );

qutil.cpp

#include "qutil.h"
template <class T>
bool isPrime( T number )
{
    // 0 and 1 are not prime
    // even numbers are not prime
    if ( number < 2 || number % 2 == 0 )
        return false;
    // now we restrict our search to odd numbers
    // greater than 2
    for ( T i = 3; i < number; i += 2 ) {
        if ( number % i == 0 )
            return false;
    }
    return true;
}

main_qutil.cpp

#include "qutil.h"
...
quint64 num = 10;
qDebug() << num << "isPrime:" << isPrime( num );

code is compiled without errors:

cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"c:\Dev\QT484\include\QtCore" -I"c:\Dev\QT484\include\QtGui" -I"c:\Dev\QT484\include" -I"." -I"c:\Dev\QT484\include\ActiveQt" -I"debug" -I"c:\Dev\QT484\mkspecs\win32-msvc2008" -Fodebug\ @C:\DOKUME~1\Alain\LOKALE~1\Temp\nm124D.tmp qutil.cpp main_qutil.cpp

but linker complains:

Generating Code... link /LIBPATH:"c:\Dev\QT484\lib" /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /MANIFEST /MANIFESTFILE:"debug\qutil.intermediate.manifest" /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='' processorArchitecture=''" /OUT:debug\qutil.exe @C:\DOKUME~1\Alain\LOKALE~1\Temp\nm124E.tmp main_qutil.obj : error LNK2019: unresolved external symbol "bool __cdecl isPrime(unsigned __int64)" (??$isPrime@_K@@YA_N_K@Z) referenced in function _main

Any idea?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Alain Weiler
  • 139
  • 1
  • 11
  • Have you tried using `isPrime(num)`? You probably have a declaration for a different, non-templated function isPrime somewhere. – Timo Geusch Sep 03 '14 at 23:28
  • I just have tried, but witout success Timo! – Alain Weiler Sep 03 '14 at 23:38
  • I also have some more functions like getNextPrime(), getPreviousPrime() which generate the same error. – Alain Weiler Sep 04 '14 at 06:03
  • Try renaming your function to something like `myIsPrime` or similar. The function the linker is complaining about isn't a templated function so you have some sort of naming conflict somewhere and most likely have the compiler prefer the non-template version over the template version. – Timo Geusch Sep 04 '14 at 15:06
  • renaming didn't work – Alain Weiler Sep 04 '14 at 21:41

1 Answers1

1

I don't think you can put a template function declaration in a header and the implementation in a cpp file. You'll have to put the entire template function in the header file (qutil.h).

Similar topic: Storing C++ template function definitions in a .CPP file

Community
  • 1
  • 1
Larry B
  • 194
  • 14
  • yes, this helped! I am surprised because in the same project I am using other templated function declared in an header file and implemented in a cpp file. – Alain Weiler Sep 04 '14 at 21:42
  • @AlainWeiler: You aren't. The C++ compiler cannot instantiate a template, unless the entire implementation is visible at the point of instantiation. If you have split your template across a header and implementation, you are either including that implementation file or you aren't instantiating the template. Those are the rules, and you cannot change them. – IInspectable Dec 30 '16 at 17:12