Template problem

This is a discussion on Template problem within the C++ Programming forums, part of the General Programming Boards category; I had the following class: Code: /*! * \brief Provides text drawing-methods for OpenGL. */ class ZTextDrawer2 { public: /* ...

  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
    22,265
    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)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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, 08:34 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21