簡體   English   中英

模板功能鏈接器錯誤LNK2019

[英]template function linker error LNK2019

在以下情況下出現鏈接錯誤,找不到缺陷所在:

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 );

代碼編譯沒有錯誤:

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

但是鏈接器抱怨:

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

任何想法?

我認為您不能將模板函數聲明放在標頭中,而實現可以放在cpp文件中。 您必須將整個模板函數放在頭文件(qutil.h)中。

相似的主題: 在.CPP文件中存儲C ++模板函數定義

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM