Thread: Template problem

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    27

    Template problem

    I had the following class:
    Code:
    /*!
     * \brief Provides text drawing-methods for OpenGL.
     */
    class ZTextDrawer2
    {
        public:
        /*
            various stuff
            ..........
            ..........
        */
           
        void Draw2DText(DrawPoint<float> p, const char *format, ...);
        void Draw3DText(DrawPoint<float> p, const char *format, ...);
    
        private:
        /*
            various stuff
            ..........
            ..........
        */
    }
    I had not problems with it, and it worked as it should be. Problems rised when I
    decided it t change it to the following:
    Code:
    /*!
     * \brief Provides text drawing-methods for OpenGL.
     */
    class ZTextDrawer2
    {
        public:
        /*
            various stuff
            ..........
            ..........
        */
           
        template<typename Scalar> void Draw2DText(DrawPoint<Scalar> p, const char *format, ...);
        template<typename Scalar> void Draw3DText(DrawPoint<Scalar> p, const char *format, ...);
    
        private:
        /*
            various stuff
            ..........
            ..........
        */
    }
    In both cases the class is used like that:
    Code:
    ZTextDrawer2::GetInstance()->Draw2DText(DrawPoint<float> (0.0f, 0.0f), "ZTextDrawer2");
    The problem with the second version of the class is that I get link errors(i use GCC):
    Code:
    obj\test.o:test.cpp|| undefined reference to `void Z_GLC::ZTextDrawer2::Draw2DText<float>(Z_GLC::DrawPoint<float>, char const*, ...|
    Why is this link error caused?

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    EDIT:

    The DrawPoint struct is defined as following:

    Code:
    template<class T> struct DrawPoint
    {
        T x, y;
    
        DrawPoint() : x(0), y(0) {}
        DrawPoint(T _x, T _y) : x(_x), y(_y) {}
    };

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to read this FAQ on Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file? (though the question of really about the definition of a function templates, rather than definition of a class template)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template already instantiated
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2010, 12:42 PM
  2. Assignment issues
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2010, 12:55 PM
  3. Reference Counting
    By Dae in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2009, 07:34 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM