Thread: Help with classes & strings members

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

    Help with classes & strings members

    I have a class and want one of hte private members to be a string, but im having trouble initializeing it in the constructor function.

    here is the class
    Code:
    // Player class declaration
    class Player
    {
     private:
             char*     playerName;
             short int hitpoints;
             short int strength;
             short int agility;
             short int magic;
             short int gold;
     public:
             Player();                  // constructor
                                        // no destructor
             void reportStats();        // shows stats
             void attack();             // kill monster
             void move();               // move N, E, S, or W (do i need parameters?)
             void rest();               // sleep to regain health
             void setPlayerInfo();      // name, sex
             void showPlayerInfo();         // for debugging only
    };
    here is the constructor
    Code:
    Player::Player()    // constructor definition
    {
     playerName = "Brian";
     strength = 10;
     agility = 10;
     hitpoints = 10;
     magic = 20;
     gold = 20;
    }
    is this how i am supposed to declare that data member and initialize it? i get weird errors and funny things when i try to display the data member.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    your char * has no memory associated with it. try something like this:
    Code:
    playerName = new char[20];
    strcpy(playerName,"Brian");
    and delete it in the destructor.

    you could use the string class to make it easier though.

    std::string playername;

    playername = "Brian";

    std::cout << playername.c_str() << std::endl;

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    where in my code do i playce those two lines?
    playerName = new char[20] in the class declaration?
    strcpy(playerName,"Brian") in the constructor?

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Both lines of code go in the constructor. But you must remember to free the memory that you have allocated using the new operator by using the delete operator.

    Therefore in your destructor you would have a line such as:

    delete [] playerName;
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    thankyou .. i think that will help with the constructor

    what goes in the class declaration though?

    char playerName;

    ?

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    What you had originally is correct:

    char *playerName;
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    thankyou foniks

    do you know of a website that teaches pointsers thoroughly ? all the websites tutorials i've seen don't make much sense and don't explain when i should/shoudn't/have to use pointers.

  8. #8
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Um.. I'm at work at the moment so I can't spend to much time looking ..

    But you could try here .

    There is also a tutorial on this site regarding pointers over here. Don't worry too much, pointers can be a little difficult at first, but you'll get it eventually. If you have any more questions, post some code.

    Oh, and search this forum, a lot of questions regarding pointers have been asked already.
    Last edited by foniks munkee; 07-21-2002 at 08:36 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. High Schoolers - Techy Classes?
    By mart_man00 in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 05-09-2003, 02:14 PM
  3. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM