首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对模板成员的奇怪“未定义引用”

对模板成员的奇怪“未定义引用”
EN

Stack Overflow用户
提问于 2011-10-14 15:04:18
回答 2查看 437关注 0票数 1

首先,我知道必须在头文件中实现定义,这就是我通常根据this thread所做的

我有几个文件

代码语言:javascript
复制
// 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-file,我就会收到以下消息:

代码语言:javascript
复制
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而不是,是一个模板化的类(它只是在扩展一个类),所以我不理解这个问题。

知道吗??

你好,托拜厄斯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-14 15:06:10

您没有为Image(int width, int height, Pixel emptyFill);提供定义,只为Image(int width, int height);提供定义。

票数 7
EN

Stack Overflow用户

发布于 2011-10-14 15:08:21

错误消息中没有给出构造函数的定义。

代码语言:javascript
复制
Image(int, int, unsigned short)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7769600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档