2011-03-08 38 views
0

我在Visual C++應用程序中修改了一個現有類,使其成爲泛型類。此類由另外兩個模塊使用。當我分別編譯這些模塊時,一切正常。C++泛型類鏈接錯誤

1>Linking... 
1> Creating library C:\ccmdb\prep\g3_v5.3\g3_tools\fc_tools\ZFRTool\ZStackMTI\Debug\ZStackMTI.lib and object C:\ccmdb\prep\g3_v5.3\g3_tools\fc_tools\ZFRTool\ZStackMTI\Debug\ZStackMTI.exp 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::CRingBuffer<unsigned char>(unsigned long)" ([email protected]@@[email protected]@Z) referenced in function "public: __thiscall CSerialPort::CSerialPort(void)" ([email protected]@[email protected]) 
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::CRingBuffer<unsigned char>(unsigned long)" ([email protected]@@[email protected]@Z) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::~CRingBuffer<unsigned char>(void)" ([email protected]@@[email protected]) referenced in function "public: void * __thiscall CRingBuffer<unsigned char>::`scalar deleting destructor'(unsigned int)" ([email protected]@@[email protected]) 
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::~CRingBuffer<unsigned char>(void)" ([email protected]@@[email protected]) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: unsigned long __thiscall CRingBuffer<unsigned char>::GetBufferItems(void)" ([email protected][email protected]@@QAEKXZ) referenced in function "public: unsigned long __thiscall CSerialPort::GetBufferedRxItems(void)" ([email protected]@@QAEKXZ) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Peek(unsigned char *,unsigned long,unsigned long *)" ([email protected][email protected]@@[email protected]) referenced in function "public: unsigned long __thiscall CSerialPort::Peek(unsigned char *,unsigned long,unsigned long *)" ([email protected]@@[email protected]) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Remove(unsigned char *,unsigned long,unsigned long *)" ([email protected][email protected]@@[email protected]) referenced in function "public: unsigned long __thiscall CSerialPort::Read(unsigned char *,unsigned long,unsigned long *)" ([email protected]@@[email protected]) 
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Remove(unsigned char *,unsigned long,unsigned long *)" ([email protected][email protected]@@[email protected]) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Add(unsigned char *,unsigned long,int)" ([email protected][email protected]@@[email protected]) referenced in function "public: unsigned long __thiscall CSerialPort::Write(unsigned char *,unsigned long)" ([email protected]@@[email protected]) 
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Add(unsigned char *,unsigned long,int)" ([email protected][email protected]@@[email protected]) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Delete(unsigned long)" ([email protected][email protected]@@[email protected]) referenced in function "public: unsigned long __thiscall CSerialPort::Delete(unsigned long,int)" ([email protected]@@[email protected]) 
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Flush(void)" ([email protected][email protected]@@QAEHXZ) referenced in function "public: unsigned long __thiscall CSerialPort::Flush(int)" ([email protected]@@[email protected]) 
1>ZStackMTI.obj : error LNK2019: unresolved external symbol "public: unsigned char __thiscall CRingBuffer<unsigned char>::Peek(unsigned long)" ([email protected][email protected]@@[email protected]) referenced in function "unsigned long __cdecl ZBNConnect_GetExtAddr(unsigned char *)" ([email protected]@[email protected]) 
1>C:\ccmdb\prep\g3_v5.3\g3_tools\fc_tools\ZFRTool\ZStackMTI\Debug\ZStackMTI.dll : fatal error LNK1120: 9 unresolved externals 
1>Build log was saved at "file://c:\ccmdb\prep\g3_v5.3\g3_tools\fc_tools\ZFRTool\ZStackMTI\ZStackMTI\Debug\BuildLog.htm" 
1>ZStackMTI - 14 error(s), 0 warning(s) 

我使用Visual Studio 2008。有誰知道如何解決這個問題:然而,當我做了建立所有,我得到這些鏈接錯誤?在修改之前,所有內容和鏈接都沒有任何問題。謝謝。

+0

也許發佈一些類和調用者的代碼? – Cosmin 2011-03-08 19:10:17

回答

6

在頭文件中實現您的模板函數。

當編譯器需要從模板創建實際類時看到整個類模板。見例如Why should the implementation and the declaration of a template class be in the same header file?

+0

這將如何解決問題? – 2011-03-08 19:11:22

+0

模板類是* not *類,它是*模板*。當編譯器使用'Foo '看到你時,它會根據模板代碼創建一個實際的類。如果編譯器在創建這個類時只能看到一個'Foo'成員函數*聲明*,它只會創建一個'Foo '成員函數聲明。如果它能看到定義,它也會創建一個定義。 – Erik 2011-03-08 19:14:32

+0

我是否應該將模板函數定義剪切並放在頭文件的末尾,模板類是在哪裏定義的?或者,它們是否需要嵌套在類中,類似於定義Java或C#類的方式? – 2011-03-08 19:19:17

0

這可能是因爲鏈接器無法找到具體類的方法體。

當您使用聲明模板化類的具體實例時,一種解決方法是包括實際的.cpp文件而不是.h文件。

+0

感謝您的解決方法。我一定要記住它。 – 2011-03-08 19:27:57