class room {
public:
int rmID; // Room ID
string rmDesc; // Room description
int rmExits[6]; // Room exit array
room(int a, string b, int c[6]){ // Constructor
rmID = a;
rmDesc = b;
rmExits[6] = c[6];
};
};
The array part doesn''t work. I don''t get an error when I create the constructor within the class, but when I try to invoke it, I don''t know what the syntax is. I''ve tried:
room Galley(1,"The kitchen.",[0 1 0 0 0 0]) and
room Galley(1,"The kitchen.",0 1 0 0 0 0)
Neither work. If I remove the array stuff from the constructor and the instantiation the other two variables work fine.
Any ideas? I can''t find a reference to this sort of thing in my C++ book.
Thanks for helping a newbie,
- gollumgollum
NEWBIE - Help with constructor and array...
OK, just to make sure you guys get enough posts in this forum... :-)
I have the following class:
November 17, 2000 12:30 PM
You have some problems here.
First off string? Is this a standard C++ thingie or are you just typedefing (char*). If you are just typefing char* you will have problems here too.
rmExits[6] = c[6] is assigning the 6th element of the object variable to the one passwd on the paramater list.
Secong is you need to copy the array into your object and not just an assignment ( same with char* ). Otherwise you will end up with your object pointing to stack variables ( this is bad ).
room( int a, char *b, int *c ) {
rmID = a;
strcpy(rmResc, b);
for ( i=0, i<6; i++ ) {
rmExits = c;<br> }<br>}<br><br>Will work better for what you want.<br><br><br> </i>
First off string? Is this a standard C++ thingie or are you just typedefing (char*). If you are just typefing char* you will have problems here too.
rmExits[6] = c[6] is assigning the 6th element of the object variable to the one passwd on the paramater list.
Secong is you need to copy the array into your object and not just an assignment ( same with char* ). Otherwise you will end up with your object pointing to stack variables ( this is bad ).
room( int a, char *b, int *c ) {
rmID = a;
strcpy(rmResc, b);
for ( i=0, i<6; i++ ) {
rmExits = c;<br> }<br>}<br><br>Will work better for what you want.<br><br><br> </i>
Ok, I am extremely new at all of this, so don't put anything past me. However, for the strings, I included the headers iostream and string. The string assignment works; I've tried it.
On to the array. Am I correct in saying that this is basically how to do it? -->
Then to invoke:
Is that right? If so, is it any better than just setting the array after I create the instance, like this? -->
Seems like the same amount of typing.
Sorry if I'm being obtuse here. I am new at this, and I appreciate your help.
- gollumgollum
Edited by - gollum on November 17, 2000 2:14:57 PM
On to the array. Am I correct in saying that this is basically how to do it? -->
int arExits[6] = {0,1,0,0,0,0};class room { public: int rmID; // Room ID string rmDesc; // Room description int rmExits[6]; // Room exit array room(int a, string b, int *c){ // Constructor rmID = a; rmDesc = b; for ( i=0, i<6; i++ ) { rmExits = c; }; };};
Then to invoke:
room Galley(1,"The kitchen",arExits);
Is that right? If so, is it any better than just setting the array after I create the instance, like this? -->
Galley.rmExits = {0,1,0,0,0,0};
Seems like the same amount of typing.
Sorry if I'm being obtuse here. I am new at this, and I appreciate your help.
- gollumgollum
Edited by - gollum on November 17, 2000 2:14:57 PM
November 17, 2000 01:21 PM
easiest way for the array would be the for loop
for(int i=0; i<6; i++)
{
rmExits = c;<br>}<br>this way you''re saying each element of the array rmExits is being assigned the corresponding value of whatever is in array c. </i>
for(int i=0; i<6; i++)
{
rmExits = c;<br>}<br>this way you''re saying each element of the array rmExits is being assigned the corresponding value of whatever is in array c. </i>
Ok. You''re close.
|
Note:
- ; after the x=0 not a ,
- rmExits[ x ] = c[ x ] copies all elements from c into rmExits.
- no ; after the for loop
- no ; at the end of room(...)
quote:
Then to invoke:room Galley(1,"The kitchen",arExits);
Is that right? If so, is it any better than just setting the array after I create the instance, like this? -->Galley.rmExits = {0,1,0,0,0,0};
Seems like the same amount of typing.
You can to it either way. The first way is perfered because it allows you to keep rmExits hidden away which is why we use classes in the first place.
-------
Andrew
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement