Archived

This topic is now archived and is closed to further replies.

gimp

Casting Problem

Recommended Posts

My error message is: c:\ZZZ\Visual Studio Projects\Game\Engine\World.inl(72) : error C2664: ''COctant::Insert'' : cannot convert parameter 1 from ''CLightNode *'' to ''CNode *'' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast The problem here is that CLightNode derives from CNode. I should be able to do this shouldn''t I? Downcasting like this... Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
class CLightNode : public CNode

Yep, CLightNode only derives from CNode., Yep it''s public.

Here is the function definition that trips the error:

void InsertLight(CLightNode* a_Light);

To me, it all looks good. a simple case of downcating, you shouldn''t event need to type anything, let alone a C ctyle cast...



Chris Brodie
http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
Downcasting? wtf is that? making up terms?

Anyways...for example

if you have an array called

CNode* base_array[100];

you can attach any of the classes DERIVED from CNode.

You can''t do this

CLightNode* derived_array[100];

and then start attaching CNode''s (it''s base class) to it with out re-casting it.

You''ll have to type cast unless you changed the function to this:
void InsertLight(CNode* a_Light);

Which of course is counter-inituative, so your stuck jsut type casting it.


-Pac
"The thing I like about friends in my classes is that they can''t access my private members directly."
"When listening to some one tell about their problem (whether it''s code or not), don''t listen to what went right or wrong, but what they assumed....."

Share this post


Link to post
Share on other sites
Downcasting is a term I picked up I think from null_pointer and a few others around here that describes casting within a derivation hierarchy in the direction of the base class, likewise upcasting was from a base to a derived class, which for example you''d usually use a dynamic cast for. Side casting was for casting between two class derived from a common base (though I can''t see why you''d ever do that, eeck).

In any case I solved the problem. It was unable to resolve the cast as I forward declared CLightNode in World.h and then used it in World.cpp without including LightNode.h.

Silly me...

Chris Brodie
http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
Upcasting = casting the object UP the inheritance list to a super class.

Downcast = casting the object DOWN the inheritance list to a sub class.

Share this post


Link to post
Share on other sites
Your error message says you can''t convert from CLightNode* to CNode*, but your function takes a CLightNode*. Did you copy-paste the error exactly, or did you type it and reverse them? Because you can''t pass a CNode* into a function that takes a CLightNode* without explicit casting. In other words, downcasting is NOT automatic, only upcasting.

  
class CLightNode : public CNode
{ /* ... */ };

void fNode (CNode *pNode);
void fLightNode (CLightNode *pLNode);

int main ()
{
CLightNode *pLightNode = new CLightNode; // our 1 object

CNode *pNode = pLightNode; // casting up, no problem

fNode (pNode); // no problem

fNode (pLightNode); // CLightNode* is-a CNode*, no problem

fLightNode (pLightNode); // ok

fLightNode (pNode); // ERROR: Cannot convert from CNode* to

// CLightNode*

fLightNode (static_cast<CLightNode *> (pNode)); // ok

return 0;
}


Share this post


Link to post
Share on other sites