引用模板类时,出现下面错误
error LNK2019: unresolved external symbol "public: __thiscall CImageSegment<short>::CImageSegment<short>(void)" (??0?$CImageSegment@F@@QAE@XZ) referenced in function "public: void __thiscall CSegmentImgTestDlg::OnBnClickedButtonSeg(void)" (?OnBnClickedButtonSeg@CSegmentImgTestDlg@@QAEXXZ)
1>K:\myProgram\testFile\SegmentImgTest\Debug\SegmentImgTest.exe : fatal error LNK1120: 1 unresolved externals。
看起构造函数的声明和定义都存在,但是依然发生链接错误。
查网参考http://zhidao.baidu.com/question/417632742.html
模板类IDE不支持分离定义的。如果.h和.cpp是分离的需要 include .cpp文件,这样在其他文件中包含.cpp文件觉得怪怪的。
第二种解决方案:
.cpp 中添加宏定义,将.cpp 作为一个宏定义。在.h文件中包含这个宏定义。
#ifndef _IMAGESEGMENT_CPP_
#include "ImageSegment.cpp"
#endif
///.cpp 文件
#include "stdafx.h"
#ifndef _IMAGESEGMENT_CPP_
#define _IMAGESEGMENT_CPP_
#include "ImageSegment.h"
template <class ImageType >
CImageSegment<ImageType>::CImageSegment(void)
{
}
template <class ImageType >
CImageSegment<ImageType>::~CImageSegment(void)
{
}
template <class ImageType >
int CImageSegment<ImageType>::GetVolumeImageROI( ImageType *pBWVol, int nWidth, int nHeight, int nSlice, POINT *leftTop, POINT *bottomDown )
{
......................
return 0;
}
#endif
/
。h文件
#include <Windows.h>
template <class ImageType>
class CImageSegment
{
public:
CImageSegment(void);
~CImageSegment(void);
public:
int GetVolumeImageROI(ImageType *pBWVol, int nWidth, int nHeight, int nSlice, POINT *leftTop, POINT *bottomDown );
public:
int BWimage(ImageType *pSrcVol, double dValMin,double dValMax,const double thredshold, long nPixelNum,ImageType *pBWVol);
};
//the IDE is not support template class definision in different file.
#ifndef _IMAGESEGMENT_CPP_
#include "ImageSegment.cpp"
#endif
///
还有一种方法:
把Cpp文件改为inl(内联)文件
并在.h 中包含#include “***.inl”