-
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::DrawWheel' : 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...
-
Does DrawWheel() processing of the parameter exactly the same for all six parameter templates?
Kuphryn
-
-
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
-
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);
}
}
-
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