Thread: Learning how to use classes and create header files

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Learning how to use classes and create header files

    Here is my header file with the class definitions
    Code:
    #ifndef CHARACTER_H
    #define CHARACTER_H
    
    class Player
    {
    private:
           int m_Attack;
           int m_Strength;
           int m_Armour;
           string m_Name;
    public:
           void SetName();
           void SetAttack(int attack);
           void SetStrength(int strength);
           void SetArmour(int armour);
           int GetAttack();
           int GetStrength();
           int GetArmour();
           string GetName();
    };
    
    #endif
    Here's the header file with the classes implementations
    Code:
    #include <iostream>
    #include "character.h"
    
    void Player::SetAttack(int attack)
    {
        m_Attack = attack;
    }
    
    void Player::SetStrength(int strength)
    {
        m_Strength = strength;
    }
    
    void Player::SetArmour(int armour)
    {
         m_Armour = armour;
    }
    
    void Player::SetName()
    {
         string temp;
         cout << "What is the name of your character? ";
         getline(cin, temp);
    
         m_Name = temp;
    }
    
    int Player::GetAttack()
    {
        return m_Attack;
    }
    
    int Player::GetStrength()
    {
        return m_Strength;
    }
    
    int Player::GetArmour()
    {
        return m_Armour;
    }
    
    string Player::GetName()
    {
           return m_Name;
    }
    and here's the main() file
    Code:
    #include <iostream>
    #include "character.h"
    
    using namespace std;
    
    int main()
    {
        int temp;
        Player Bob;
        
        Bob.SetName();
        
        cout << "\nWhat attack rating is your character? ";
        cin >> temp;
        Bob.SetAttack(temp);
    
        cout << "\nWhat strength rating is your character? ";
        cin >> temp;
        Bob.SetStrength(temp);
    
        cout << "\nWhat armour rating is your character? ";
        cin >> temp;
        Bob.SetArmour(temp);
        
        cout << "\n" << Bob.GetName() << " has an attack rating of " << Bob.GetAttack() << ", a strength rating of " << Bob.GetStrength() << ", and an armour rating of " << Bob.GetArmour() << ".";
    
    cin.ignore(4);
    return 0;
    }
    I get two errors for the class definitions file saying 'string does not name a type', have i created the class wrong or what? im baffled.

    P.S. sorry for the long post

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Include string in your header file. Otherwise, your compiler doesn't know what a string is.

    Also, it'd be a good idea to implement the default constructor of your class so that that behavior won't be undefined.

  3. #3
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Have included string header file but it still doesnt compile. I know my compiler uses string because i've used it plenty of times before. My compiler is bloodshed 4.9.9.2, is there maybe a known issuse with private strings in classes?

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    problem solved, didnt need the string header file, i needed to declare
    Code:
    using namespace std;
    in the class declaration file

    P.S. sorry for a double post, forgot about the editing ability, ill edit my last post next time

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here's the header file with the classes implementations
    Class implementations go in a .cpp file not a .h file.

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    it is a .cpp file, the class definitions file is the only .h file, simple typo, my mistake

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Have included string header file but it still doesnt compile.
    Instead of saying "it doesn't work" or "it doesn't compile", next time post the error message AND on the line with the error put a big red "THIS IS WHERE THE ERROR IS". Then, it will be much easier for someone looking at your code to tell you what's wrong.

  8. #8
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Quote Originally Posted by Welshy
    I get two errors for the class definitions file saying 'string does not name a type'
    Did i not just quote the error message there

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Welshy
    Did i not just quote the error message there
    uuuhhh, no:
    I get two errors for the class definitions file saying 'string does not name a type',
    Include string in your header file.
    Have included string header file but it still doesnt compile.
    ...why? what errors caused it not to compile? what lines were the errors on(not the line number, but a clear mark in the code you posted indicating the line with the error)? An error message with a mark where it occurred in your code can be immediately diagnosed.
    Last edited by 7stud; 04-19-2005 at 05:04 AM.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    7stud, give it a rest.

    This question was answered before you contributed.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This question was answered before you contributed.
    This is where the thread was when I started looking at the problem:
    Have included string header file but it still doesnt compile. I know my compiler uses string because i've used it plenty of times before. My compiler is bloodshed 4.9.9.2, is there maybe a known issuse with private strings in classes?
    ...and I posted my answer, but as often happens another post got in ahead of mine.

    Is it not good policy to post the errors and where they occur in the code? Doesn't that save a lot of time? I dunno, I thought it was common policy to inform newer members the best way to get their problems solved, and at the same time not waste other people's time.
    Last edited by 7stud; 04-19-2005 at 12:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  2. Create generic classes to be specified later?
    By jmd15 in forum C++ Programming
    Replies: 18
    Last Post: 08-05-2007, 09:21 AM
  3. How to use classes in seperate C++ files
    By dxfoo in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 05:55 PM
  4. Can not reach the classes and create instances of them
    By kromozom in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2005, 07:31 AM
  5. doin' classes in header files?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 03:56 AM