Thread: Trouble with inheritance.....

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Unhappy Trouble with inheritance.....

    Hi all,

    I'm writing a program which uses inheritance but it won't compile! I believe the constructor of the base class is to blame, but I'm not sure as I've not had much experience with inheritance.

    Please find 5 of my code files attached ( 5 is maximum allowed ) and the last 2 below for perusal.

    Code:
    // userControlledCharacterClass.h
    #ifndef USERCONTROLLEDCHARACTERCLASS_H
    #define USERCONTROLLEDCHARACTERCLASS_H
    #include "RPG.h"
    
    
    class userControlledCharacterClass: public RPG
    {
    public:
        userControlledCharacterClass(); // constructor
        userControlledCharacterClass( string, string, int, int, int, int ); // constructor
        ~userControlledCharacterClass(); // destructor
    protected:
    private:
    };
    
    #endif // USERCONTROLLEDCHARACTERCLASS_H
    Code:
    // userControlledCharacterClass.cpp
    #include "userControlledCharacterClass.h"
    using namespace std;
    
    userControlledCharacterClass::userControlledCharacterClass()
    {
    
    } // end constructor userControlledCharacterClass
    
    userControlledCharacterClass::userControlledCharacterClass( string a, string b, int c, int d, int e, int f )
    {
        setName( a );
        setDescription( b );
        setXP( c );
        setHP( d );
        setAP( e );
        setLocation( f );
    } // end constructor userControlledCharacterClass
    
    userControlledCharacterClass::~userControlledCharacterClass()
    {
        cout << getName() << " has died!1" << endl;
    } // end destructor ~userControlledCharacterClass()
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JM1082
    I'm writing a program which uses inheritance but it won't compile!
    What is the exact error message, and where is it detected?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    Detected in line: 5 & 10 of userControlledCharacterClass.cpp
    Error message reads: Undefined reference to RPG::RPG()

    Strangely, this error is detected twice at each line.....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to define the default constructor for RPG.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    Thanks Laserlight! Your sharp eye is only exceed by your majesty!

    However, I've two last questions!

    Is it compulsory to include a constructor with an empty arguments list?

    And, why are seven objects of each class destroyed when I've only created three?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JM1082
    Is it compulsory to include a constructor with an empty arguments list?
    No. However, if you do not have a default constructor for a base class, then the constructors of classes that derive from the base class would need to specify which base class constructor to invoke in their initialiser lists.

    Quote Originally Posted by JM1082
    And, why are seven objects of each class destroyed when I've only created three?
    Maybe you did not notice that you were doing some copying.

    Off topic: I just heard that Barbara Liskov, after whom the Liskov substitution principle is named, is in Singapore right now giving a talk at my alma mater. Pity I am unable to attend.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GDB and inheritance
    By markcole in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2009, 09:19 AM
  2. Inheritance trouble
    By stillwell in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2006, 09:37 PM
  3. Inheritance
    By mrafcho001 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2006, 03:49 PM
  4. Help with inheritance
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2002, 06:12 PM
  5. Someone help me a bit here. [ inheritance ]
    By rmullen3 in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2002, 01:08 AM