Thread: Default Constructors w/Classes as Parameters

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Default Constructors w/Classes as Parameters

    Confusing title, confusing question.
    First off, I'm making arrays of type Room (class). I'm aware I have to have default constructor arguments for this to work... or whatever you call it. My problem is, I can't seem to give a default value to one of the parameters in my constructor function; and that parameter type happens to be another class.

    The constructor header:
    Code:
    Room::Room(int dr = 0, int dc = 0,                   // row, column number (d for default arguments)
               bool dN = true, bool dE = true,           // exits (N, E)
               bool dS = true, bool dW = true,           // exits (S, W)
               char* dname = '\0', char* ddescr = '\0',  // name, description
               Monster dmob)
    Okay Dokay - since I am creating an array of these Room types, I need default constructor arguments.. and I have them there.. all except for the last parameter (Monster dmob). How can I give this a default value?

    If this doesn't make sense please say so, and i'll post the rest of the code.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    maybe you could use a pointer to a Monster object and make it NULL in the constructor?
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    hmm.. that sounds like it will work. but i dont know what any of that means... 'specially that null pointer (i a newb)
    Could you explain in a nutshell what that is?
    Is it something like this?
    Code:
    Room::Room(........,Monster* p_Monster = NULL)
    {
     // ....
     thePrivateDataMemberOfTypeMonster = *p_Monster;
    }
    Last edited by GrNxxDaY; 07-31-2002 at 05:34 AM.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    yup, that looks correct
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Wow, thanks endo!!
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Of course if you are making a default constructor, then you know you can just create one with no arguments right? eg
    Code:
    Room::Room( )
    {
       dr = dc = 0;
       dN = dS = dW = dE = true;
       dname = ddesc = NULL;
       //no need to initialise Monster, its defualt constructor should do that
    }
    Anyway, its just another idea...
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    (kind off subject)
    *Wants to be as smart as endo some day*
    What book do you reccommend for beginners endo? That C++ 21 days seems popular...
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  8. #8
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I aint that smart, if I was I'd have a programming job

    Anyway, I've got "C++ How to Program", third ed - Deitel & Deitel. Thats a good book which seemed really complex when I started but I don't think you'll struggle with it at all. It covers loads of stuff as well, still not read the whole book...

    Edit : And it comes with the intro version of Visual C++ 6, which is nice.
    Last edited by endo; 07-31-2002 at 05:52 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

  9. #9
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Thankyou
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  10. #10
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Doh. It compiled just fine with no errors, but now when I try to run the .exe, i get illegal operation! *whine*
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  11. #11
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Is it definitly because of the constructor? post the code.
    Couldn't think of anything interesting, cool or funny - sorry.

  12. #12
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    it is very big.
    should i upload it or post it?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  13. #13
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Okay here's the whole thing.

    The classes are in classes.h
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  14. #14
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I have errors telling me there is no appropriate default constructor for Room and Monster Objects. I have removed your Monster constructor and put this instead, which doesn't produce errors

    Code:
    Monster::Monster( )
    {
    	maxHP = curHP = str = agi = gold = xp = 0;
    	alive = false;
    	name = art = Art = attack = dodge = NULL;
    }
    I can fix the whole lot if you like but the Room constructor is going to be very similar, so if this isn't what you want say so now.

    For the Room class you will also need to overload the assignment(=) operator for the Monster class because you'll get dodgy results otherwise.
    Last edited by endo; 07-31-2002 at 06:46 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

  15. #15
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Sounds good to me! I'm always looking for pointers at c++ (not the pointers to addresses)

    I'm not quite sure what overloaded assignment is.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2008, 12:46 AM
  2. Default Constructors
    By pritin in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2007, 06:38 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Inherited constructors with parameters
    By dave74 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2003, 11:27 AM
  5. Default Constructors
    By MethodMan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:30 PM