i have function template which looks like this :
HelloWorld.h
template<typename T>
bool NodeTraverse(T* &pGameObj);
HelloWorld.cpp
template<typename T>
bool HelloWorld::NodeTraverse(T* &pGameObj)
{
}
im passing to the function 2 types sometimes A_object and Somtimes B_obejct both are implementing same interface IGame
class IGame
{
public:
virtual ObjType getType() = 0;
virtual void setType(ObjType ot) = 0;
protected:
ObjType objtype;
};
class A_object : public IGame { public: A_object(); ObjType getType() { return objtype; } void setType(ObjType ot) { objtype = ot; } }
class A_object : public IGame
{
public:
A_object();
virtual ~A_object();
ObjType getType() { return objtype; }
void setType(ObjType ot) { objtype = ot; }
}
class B_object : public IGame
{
public:
B_object();
virtual ~B_object();
ObjType getType() { return objtype; }
void setType(ObjType ot) { objtype = ot; }
}
now i call NodeTraverse like this :
A_object * pA_object = new A_object() ;
pA_object ->setType(A_OBJECT);
NodeTraverse(pA_object);
so far all good . but when i compile the app im getting this worrying
2> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2> d:\\classes\helloworldscene.cpp(114) : see reference to function template instantiation 'bool HelloWorld::NodeTraverse<A_object >(T *&)' being compiled
2> with
2> [
2> T=A_object
2> ]
But the problem is that im allso getting compilation error which i suspect related when i do this :
if (gameObjParent->getType() == LAYER_OBJECT)
{
pGameObj = ((A_object *)gameObjParent);
}
else if (gameObjParent->getType() == SPRITE_OBJECT)
{
pGameObj = ((B_object *)gameObjParent); // HERE IS THE COMPILATION ERROR
}
some how it thinks that gameObjParent is A_object * This is the error: it comes above the template worning
2>d:\classes\helloworldscene.cpp(200): error C2440: '=' : cannot convert from 'B_object *' to 'A_object *'
2> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2> d:\\classes\helloworldscene.cpp(114) : see reference to function template instantiation 'bool HelloWorld::NodeTraverse<A_object >(T *&)' being compiled
2> with
2> [
2> T=A_object
2> ]
the wired part is that it is compilation error . why ?