Archived

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

Gurami

C++ Inheritence / Type Casting error

Recommended Posts

Hey, I've run up to an error in MSVC++ that i've never seen before. I've got two classes, LinkedList and PlanetList, PlanetList is a child class of LinkedList, and when I attempt to assign the address of a PlanetList to a *LinkedList, i get the following error: error C2243: 'type cast' : conversion from 'class PlanetList *' to 'class LinkedList *' exists, but is inaccessible. The pertinent code is replicated below:
  
LinkedList *OnScreenList;
Object *LastClicked;
.
.
.
class LinkedList
    {
    //...LinkedList Body...

    }
.
.
.
class PlanetList : LinkedList
    {
    //...PlanetList Body...

    }
.
.
.
OnScreenList = ((StarObject*)LastClicked)->star.planets;
   
Now, i believe the explicit conversion from Object to StarObject works, because it works elsewhere in the code. Star.planets is a *PlanetList. Help! Thanks! Mike Edited by - Gurami on January 31, 2002 1:44:09 AM

Share this post


Link to post
Share on other sites
Maybe the inherited parent should be inherited public ?

class PlanetList : public LinkedList

Reg''ds,
sTeVe

Share this post


Link to post
Share on other sites
Why do you want to cast a derived class to the base class? That''s an undefined operation (because the derived class can - and most probably does - contain data/methods not present in the base class).

Anyway, try manually casting it (rather than implictly):
OnScreenList = (LinkedList *)((StarObject*)LastClicked)->star.planets; 

I expect it to fail, though.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites