You are awesome! I took out all the parameters for the default constructors for Room and Monster like you said -- works like a charm
you're good
Printable View
You are awesome! I took out all the parameters for the default constructors for Room and Monster like you said -- works like a charm
you're good
The default Room constructor:
Remember these create useless rooms, no names, descriptions, exits, monsters or anything. Same with the default Monster constructor.Code:Room::Room( )
{
r = c = 0; //unsure about what this does so I'll just set it to 0
N = E = S = W = false;
name = descr = NULL;
}
Overloaded = operators are required so that you can do things like this safely:
Just working on a bit of the overloaded = then I'll post that :)Code:Monster m1, m2;
m1.changeSomething( );
m2 = m1; //= used here
hermm.. try not to change TOO much to the point where it is unreadable by me, a n00b (see the avatar?). If you do change something, do you think you could explain? I just started C++ 3 weeks ago!.
OK but you will need it if you are going to use strings represented as char*'s. Here is part of the overloaded assignment, if its too complex say so and you should probably use string objects instead.
The compiler creates a default operator for your classes but that would just copy rvalue.name into name leaving 2 objects pointing at the same space to represent name. Therefore if you change 1 then the other would also change, not good.Code:Monster& Monster::operator =( Monster& rvalue )
{
//routine assignent of values
alive = rvalue.alive;
maxHP = rvalue.maxHP;
curHP = rvalue.curHP;
str = rvalue.str;
agi = rvalue.agi;
gold = rvalue.gold;
xp = rvalue.xp;
//create space for name attribute
name = new char[ strlen( rvalue.name )+1 ];
if( name == NULL )
{
cout << "Insufficient memory, exiting....";
exit( 0 );
}
else
{
strcpy( name, rvalue.name ); //copying name
}
return *this;
//UNFINISHED
}
By the way, your doing much better than I was after 3 weeks - I wasn't even on to pointers until week 4!
EEP - yes, that all looks very complex and the only part I understand is:
I see you have *this pointer.. I can't get that far in my C++ book cause the author expects me to already understand everything.Quote:
Code:cout << "Insufficient memory, exiting....";
& I'm hesitant to use the new[] thingy because I dont understand the benefits of it or what dynamic memory really is...
Since i'm too n00bish, i should use string types instead of char*?
you could do, but you could also use char arrays. Just declare name as char name[ 20 ] or something then treat them as char*'s after that. That way you will know exactly how much space you have every time.
or you could declare them as string objects, which takes care of most of that stuff for you.
string name;
string attack;
etc.
etc.
If you do that the set them to an empty string in the constructor:
name = "";
Hope that helps, the char arrays are more educational but string objects are simpler to use.
thanks for helping with those constructors - you saved me hours of pressing the re-build button.
i have no idea what i'm gonna do about those char*'s now though. I'd like to learn why I need all that overloaded operator stuff - but i'm too stupid to comprehend it =P.
Thanks again for spending the time to help!
No problem I remember how tough it is when you are starting. I'd suggest using string objects for now though, they are nice and versatile :)