Casting Problem

Started by
8 comments, last by gimp 22 years, 1 month ago
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
Chris Brodie
Advertisement
Is CNode the only thing CLightNode derives, if not does the other inherited class extend CNode only in it''s inheritance?
does CLightNode derive publicly from CNode ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
Chris Brodie
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....."
-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....."
Downcast is a well known term.

Is there another Insert Function that takes a CNode * pointer as it''s argument?
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
Chris Brodie
Upcasting = casting the object UP the inheritance list to a super class.

Downcast = casting the object DOWN the inheritance list to a sub class.
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;}  


Just realized I forgot to delete our object so my main leaks--but that wasn''t the point anyway. =)

This topic is closed to new replies.

Advertisement