C ++通用类链接错误

时间:2011-03-08 19:06:28

标签: c++ visual-studio-2008 linker-errors generics

我修改了Visual C ++应用程序中的现有类,使其成为泛型类。该类由另外两个模块使用。当我单独编译这些模块时,一切正常。但是,当我执行Build All时,我会收到这些链接器错误:

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)" (??0?$CRingBuffer@E@@QAE@K@Z) referenced in function "public: __thiscall CSerialPort::CSerialPort(void)" (??0CSerialPort@@QAE@XZ)
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::CRingBuffer<unsigned char>(unsigned long)" (??0?$CRingBuffer@E@@QAE@K@Z)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::~CRingBuffer<unsigned char>(void)" (??1?$CRingBuffer@E@@QAE@XZ) referenced in function "public: void * __thiscall CRingBuffer<unsigned char>::`scalar deleting destructor'(unsigned int)" (??_G?$CRingBuffer@E@@QAEPAXI@Z)
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: __thiscall CRingBuffer<unsigned char>::~CRingBuffer<unsigned char>(void)" (??1?$CRingBuffer@E@@QAE@XZ)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: unsigned long __thiscall CRingBuffer<unsigned char>::GetBufferItems(void)" (?GetBufferItems@?$CRingBuffer@E@@QAEKXZ) referenced in function "public: unsigned long __thiscall CSerialPort::GetBufferedRxItems(void)" (?GetBufferedRxItems@CSerialPort@@QAEKXZ)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Peek(unsigned char *,unsigned long,unsigned long *)" (?Peek@?$CRingBuffer@E@@QAEHPAEKPAK@Z) referenced in function "public: unsigned long __thiscall CSerialPort::Peek(unsigned char *,unsigned long,unsigned long *)" (?Peek@CSerialPort@@QAEKPAEKPAK@Z)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Remove(unsigned char *,unsigned long,unsigned long *)" (?Remove@?$CRingBuffer@E@@QAEHPAEKPAK@Z) referenced in function "public: unsigned long __thiscall CSerialPort::Read(unsigned char *,unsigned long,unsigned long *)" (?Read@CSerialPort@@QAEKPAEKPAK@Z)
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Remove(unsigned char *,unsigned long,unsigned long *)" (?Remove@?$CRingBuffer@E@@QAEHPAEKPAK@Z)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Add(unsigned char *,unsigned long,int)" (?Add@?$CRingBuffer@E@@QAEHPAEKH@Z) referenced in function "public: unsigned long __thiscall CSerialPort::Write(unsigned char *,unsigned long)" (?Write@CSerialPort@@QAEKPAEK@Z)
1>ZStackMTI.obj : error LNK2001: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Add(unsigned char *,unsigned long,int)" (?Add@?$CRingBuffer@E@@QAEHPAEKH@Z)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Delete(unsigned long)" (?Delete@?$CRingBuffer@E@@QAEHK@Z) referenced in function "public: unsigned long __thiscall CSerialPort::Delete(unsigned long,int)" (?Delete@CSerialPort@@QAEKKH@Z)
1>SerialPort.obj : error LNK2019: unresolved external symbol "public: int __thiscall CRingBuffer<unsigned char>::Flush(void)" (?Flush@?$CRingBuffer@E@@QAEHXZ) referenced in function "public: unsigned long __thiscall CSerialPort::Flush(int)" (?Flush@CSerialPort@@QAEKH@Z)
1>ZStackMTI.obj : error LNK2019: unresolved external symbol "public: unsigned char __thiscall CRingBuffer<unsigned char>::Peek(unsigned long)" (?Peek@?$CRingBuffer@E@@QAEEK@Z) referenced in function "unsigned long __cdecl ZBNConnect_GetExtAddr(unsigned char *)" (?ZBNConnect_GetExtAddr@@YAKPAE@Z)
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.有谁知道如何解决这个问题?在修改之前,所有内容和链接都没有任何问题。感谢。

2 个答案:

答案 0 :(得分:6)

在头文件中实现模板功能。

编译器在从模板创建实际类时需要查看整个类模板。参见例如Why should the implementation and the declaration of a template class be in the same header file?

答案 1 :(得分:0)

这可能是因为链接器无法找到具体类的方法体。

当您使用声明模板化类的具体实例时,一种解决方法是包含实际的.cpp文件而不是.h文件。