#1
  1. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46

    simple C++ inheritance: "inaccessible base"


    I'm stuck with a little C++ OO

    I've got a compiler error I have no idea how to fix...

    "16 C:\Dev-Cpp\rogue\main.cpp
    `Object' is an inaccessible base of `Mobile'"


    Object is the baseclass of Mobile

    (Object modelling all objects, and Mobile, the subclass, models objects that can move)


    I declared them with this code (the final line causes the error):
    Code:
      Tile * testTile = new Tile;
      Object * testObject01 = new Object("TestObject", testTile);
      Object * testObject02 = new Mobile("TestObject", testTile);

    If any other code is needed, ask and it'll be provided

    Any ideas? Thanks in advance :)
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Any chance the method is private?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    My forum experience has been shot to hell a bit... IE seems to be dying every time I try to visit a page...

    Code:
     Object * testObject02 = new Mobile("TestObject", testTile);
    This line causes the error.. .the constructor isn't private, I checked...
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    If you would, strip down your code to a small example that replicates the problem and post it. Who knows? You might even find the problem as you strip out extraneous code.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    It's got something to do with inheritance, but I can't spot it for the life of me... that's one reason I don't like C++ that much... the errors can be so crippling...

    Sorry the "code" isn't any smaller... my class structure is already very simple (and fairly self explanatory) and I'm finding it hard to isolate the problem... because I don't know where to start looking...


    Here's the basic code:

    Object (base class)

    Code:
    class Object {
     
     friend ostream& operator << (ostream & out, const Object & obj); 
     
     public:
        
        Object(
            const string & objName, 
            Tile * pos); 
            
        Object(Object &copy);  // Copy Constructor
        virtual ~Object();
     
     // Accessor functions  
    //... code here
    
    // String output function
       virtual string toString() const;
     
     private:
       string name;
       Tile * currentPosition; 
     };

    Now the derived class, Mobile:

    Mobile (derived from Object)

    Code:
    class Mobile : Object {
      public:
          Mobile(string nam, 
                 Tile * pos,
                 int mov = 1,
                 int att = 1,
                 int rang = 1,
                 int dodg = 1,
                 int str = 1,
                 int lif = 100
                ) :
                Object(nam,pos), move(mov), attack(att), range(rang),
                dodge(dodg), strength(str), lifeRemaining(lif)            
                {};
      
          virtual ~Mobile() {};
          
          Mobile(Mobile & copy) : Object(copy.getName(), copy.getPosition()){
                move = copy.getMove();
                attack = copy.getAttack();
          };
      
      // Accessors
      .... // more code here
         
      // String Output function
         
         virtual string toString() const;   
    
      private:
        int attack, move, range, dodge, strength, lifeRemaining;
       
    };


    The way the classes are related is that they share the same "operator <<" code... I want to be able to use the "toString" function of both classes to output a summary for a test harness...

    Code:
       ostream& operator << (ostream & out, const Object & obj) {
    
          string tempString = obj.toString();
          out << tempString.c_str();
          return out;
       }

    Somewhere, the inheritance is falling over, and I'm too inexperienced to find out where! :(


    I think it may have something to do with the way that the toString function of Mobile works, as it accesses non-virtual methods that don't exist in Object:

    Code:
        string Mobile::toString() const {
    
          ostringstream out;
              
              out << "\n M \t A \t R \t D \t S \t lif "
                    << "\n " << getMove() 
                    << "\t " << getAttack()
                    << "\t " << getRange()
                    << "\t " << getDodge()
                    << "\t " << getStrength()
                    << "\t " << getLifeRemaining() << endl;
                  
              return out.str();
              
          }
  10. #6
  11. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    :( I build a simpler model and it still doesn't work!
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    Can you please change the declaration of Mobile to this:
    Code:
    class Mobile : public Object {
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    ... something so simple

    sweet... thanks
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0
    Great! thanks!

    Comments on this post

    • salem disagrees : useless grave-digger

IMN logo majestic logo threadwatch logo seochat tools logo