对模板成员的奇怪"undefined reference"
Strange "undefined reference" to template members
首先,我知道必须在头文件中实现定义,这就是我通常根据这个线程
所做的。我有几个文件
// Image.h
namespace library
{
namespace data
{
template<class Pixel, class SubPixel, int Layer>
class Image
{
public:
Image(int width, int height, Pixel emptyFill);
Image(int width, int height);
~Image();
Pixel& operator() (int x, int y);
const Pixel& operator() (int x, int y) const;
SubPixel& operator() (int x, int y, int subpixel);
const SubPixel& operator() (int x, int y, int subpixel) const;
protected:
int m_width;
int m_height;
Pixel* m_pixels;
void init(Pixel emptyFill);
bool checkBounds(int x, int y, int layer = 0) const;
virtual SubPixel& pixelToSubpixel(Pixel& pixel, int layer) const = 0;
};
template<class Pixel, class SubPixel, int Layer>
Image<Pixel, SubPixel, Layer>::Image(int width, int height)
: m_width(width), m_height(height), m_pixels(new Pixel(width * height))
{
init(0);
}
template<class Pixel, class SubPixel, int Layer>
Image<Pixel, SubPixel, Layer>::~Image()
{
delete m_pixels;
}
template<class Pixel, class SubPixel, int Layer>
Pixel& Image<Pixel, SubPixel, Layer>::operator() (int x, int y)
{
// implementation ...
}
template<class Pixel, class SubPixel, int Layer>
const Pixel& Image<Pixel, SubPixel, Layer>::operator() (int x, int y) const
{
// implementation ...
}
template<class Pixel, class SubPixel, int Layer>
SubPixel& Image<Pixel, SubPixel, Layer>::operator() (int x, int y, int subpixel)
{
// implementation ...
}
template<class Pixel, class SubPixel, int Layer>
const SubPixel& Image<Pixel, SubPixel, Layer>::operator() (int x, int y, int subpixel) const
{
// implementation ...
}
template<class Pixel, class SubPixel, int Layer>
void Image<Pixel, SubPixel, Layer>::init(Pixel emptyFill)
{
// implementation ...
}
template<class Pixel, class SubPixel, int Layer>
inline bool Image<Pixel, SubPixel, Layer>::checkBounds(int x, int y, int subpixel) const
{
// implementation
}
}
}
// DepthImage.h:
namespace library
{
namespace data
{
template class Image<unsigned short, unsigned short, 1>;
class DepthImage : public Image<unsigned short, unsigned short, 1>
{
public:
DepthImage(int width, int heigth);
DepthImage(int width, int heigth, unsigned short emptyfill);
~DepthImage();
protected:
virtual unsigned short& pixelToSubpixel(unsigned short& pixel, int layer) const;
private:
DepthImage();
};
}
}
整个程序被编译成一个共享对象库。
当我试图将一个可执行对象链接到这个.so
-文件时,我收到:
library.so: undefined reference to `library::data::Image<unsigned short, unsigned short, 1>::Image(int, int, unsigned short)'
collect2: ld returned 1 exit status
我甚至想在DepthImage.h
上做一个显式的模板实例化。由于DepthImage
是不是一个模板化类(它只是扩展一个),我不明白这个问题。
任何想法?
问候,托拜厄斯
您没有提供Image(int width, int height, Pixel emptyFill);
的定义,只提供Image(int width, int height);
的定义
错误信息中没有给出构造函数的定义。
Image(int, int, unsigned short)
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 安卓 NDK 制造。数百"undefined reference error"
- 当我尝试进行TPC-E测试时,实用程序抱怨"undefined reference"
- 声明和定义函数静态会产生"undefined reference to function_name()"
- C++ : "undefined reference to"可能是因为CMake文件
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- AppData\Local\Temp\cc59LXDc.o:test1.cpp: undefined reference to 'constructNewObjectOfClass
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"