Template Question

This is a discussion on Template Question within the C++ Programming forums, part of the General Programming Boards category; I have this function Code: void cRenderEngine::DrawWheel(cSelectionWheel<sTerrainType*>* selectionWheel) { --code-- } I am going to have to duplicate the function ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Template Question

    I have this function

    Code:
    void cRenderEngine::DrawWheel(cSelectionWheel<sTerrainType*>* selectionWheel) {
        --code--
    }
    I am going to have to duplicate the function like 8 times, because the cSelectionWheel template has different types. My thought was to template this function too, but I get this error.

    Code:
    template<class T>
    void cRenderEngine::DrawWheel(cSelectionWheel<T>* selectionWheel) {
        --code--
    }
    error C2244: 'cRenderEngine:rawWheel' : unable to resolve function overload

    I am guessing that it wont let me template this function because the cRenderEngine class is not a template... Do you have any ideas as to how I can do what I want, apart from repeating the function for each type...

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Does DrawWheel() processing of the parameter exactly the same for all six parameter templates?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Yes.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    Two things you'll want to do.
    With MSVC you'll want to inline any template member functions. In other word, make the implementation and declaration one.
    Also, instead of making the template parameter of cSelectionWheel the template parameter of DrawWheel(), make the entire DrawWheel() parameter the template type:
    Code:
    #include <iostream>
    using namespace std;
    
    template <class T>
    struct cSelectionWheel
    {
        T m_t;
    
        cSelectionWheel(const T& t) : m_t(t) {}
    };//cSelectionWheel
    
    struct sTerrainType
    {
        int m_n;
    };//sTerrainType
    
    class cRenderEngine
    {
    public:
        template<class cSW_t> // cSelectionWheel<T> type
        void DrawWheel(cSW_t *pSelectionWheel)
        {
            cout << pSelectionWheel->m_t->m_n << endl;
        }//DrawWheel
    };//cRenderEngine
    
    int main()
    {
        sTerrainType tt = {42};
        cSelectionWheel<sTerrainType*> sw(&tt);
    
        cRenderEngine re;
        re.DrawWheel(&sw);
    
        return 0;
    }//main
    gg

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    would you inline this function? Ive allways read that inlines should be no more than a couple lines. Dose being a template change that guideline?

    Code:
    void cRenderEngine::DrawWheel(cSelectionWheel<sTerrainType*>* selectionWheel) {
    	list<sTerrainType*>::iterator sel;	
    	float w = 3.0f, h = 3.0f;
    
    	glLoadIdentity();
    	//translate to center
    	glTranslatef(selectionWheel->center.x,selectionWheel->center.y,selectionWheel->center.z);
    	//rotate by offset
    	glRotatef(selectionWheel->offset,1,0,0);
    
    	for(sel = selectionWheel->objectList->begin(); sel != selectionWheel->objectList->end(); sel++) {
    		glPushMatrix();
    		//translate out by radius
    		glTranslatef(0,0,selectionWheel->radius);
    		//bind preview pic
    		glBindTexture(GL_TEXTURE_2D, (*sel)->previewTexture);
    		//draw preview
    		glColor3f(1.0f,1.0f,1.0f);
    		glEnable(GL_DEPTH_TEST);
    		glBegin(GL_QUADS);
    			glVertex2f(-w,-h);	glTexCoord2f(1,0);
    			glVertex2f(w,-h);	glTexCoord2f(1,1);
    			glVertex2f(w,h);	glTexCoord2f(0,1);
    			glVertex2f(-w,h);	glTexCoord2f(0,0);
    		glEnd();
    		glDisable(GL_DEPTH_TEST);
    		glPopMatrix();
    		//rotate by step		
    		glRotatef(-selectionWheel->step,1,0,0);
    	}	
    
    }

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    The compiler is smart enough to know when to truely inline a function. Even the "inline" keyword is just a suggestion that the compiler can ignore.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 02:52 PM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 10:43 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

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