Странная "неопределенная ссылка" для членов шаблона - PullRequest
1 голос
/ 14 октября 2011

Прежде всего, я знаю, что нужно реализовать определение в заголовочных файлах, и это то, что я обычно делал в соответствии с этой веткой

У меня есть несколько файлов

// 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 не шаблонный класс (он просто расширяет класс), я не понимаю проблемы.

Есть идеи ??

С уважением, Тобиас

Ответы [ 2 ]

7 голосов
/ 14 октября 2011

Вы не предоставили определение для Image(int width, int height, Pixel emptyFill);, только для Image(int width, int height);

4 голосов
/ 14 октября 2011

Нет определения для конструктора, указанного в сообщении об ошибке.

Image(int, int, unsigned short)
...