0

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 ?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 3
    Post a minimal example, right now we have to guess where which piece of code is supposed to be... – krzaq Nov 30 '16 at 10:04
  • 1
    Generally speaking, whenever you find the need to do a C-style cast you're probably doing something you should not do. In this case I suggest you first look if you can't solve your *original* problem with polymorphism and virtual functions. – Some programmer dude Nov 30 '16 at 10:04
  • 2
    HelloWorld **.cpp** is a suspicious place to implement template method. – Jarod42 Nov 30 '16 at 10:15
  • what do you mean cpp it wired place ? – user63898 Nov 30 '16 at 10:18
  • It seems that your implementation only see forward declaration of `A_object`/`B_object` and not full definition (with inheritance with `IGame`). – Jarod42 Nov 30 '16 at 10:19
  • 2
    Look at [why-can-templates-only-be-implemented-in-the-header-file](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Jarod42 Nov 30 '16 at 10:19
  • i was looking on this post , but when i do for example : bool HelloWorld::NodeTraverse(T* &pGameObj) it doesn't work – user63898 Nov 30 '16 at 10:24
  • @Some programmer dude this is what i did , didnt know its so problematic – user63898 Nov 30 '16 at 12:24

0 Answers0