The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 6 of 6

Thread: Error E2247

  1. #1

    Error E2247

    When I try to compile my source code in Game.cpp I get there errors:
    C:\Borland\TESTSO~1>bcc32 Game.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    Game.cpp:
    Error E2247 Game.cpp 104: 'Player::HP' is not accessible in function Battle:oB
    attle(Player,Enemy)
    Error E2247 Game.cpp 109: 'Player::HP' is not accessible in function Battle:oB
    attle(Player,Enemy)
    Error E2247 Game.cpp 109: 'Player::HP' is not accessible in function Battle:oB
    attle(Player,Enemy)
    *** 3 errors in Compile ***
    This is the relevant parts of my source code, I don't have a clue why a I can't access a public variable from a method :S.

    Code:
    #include <iostream.h>
    #include <string>
    class Player
      {
      private:
        int age;
        char *name;
      public:
        int SetName(char *n);
        char *Player::GetName();
        int GetAge();
        int SetAge(int a);
        int HP;
        int Str;
        int Def;
        void DisplayStats();
        int Wins;
        int Losses;
      };
    
    class Enemy: Player
    {
        public:
        int age;
        Enemy();
        char *name;
        void DisplayStats();
    };
    
    class Battle
    {
        public:    
        Player user;
        Enemy monster;
        bool DoBattle(Player u, Enemy m);
    };
    
    // methods are cropped out for readability.
    
    bool Battle::DoBattle(Player u, Enemy m)
    {
        user  =   u;
        monster = m;
        int turn = 0;
        while(user.HP > 0 && monster.HP > 0) // errors, cant access HP property of user
        {
            if(IsEven(turn))
           {
            // User Turn
            monster.HP = monster.HP - u.Str;
           }
        else
           {
            // Monster turn
           }
            
        
        turn++;
        }
        if(user.HP > 0)
        return true;
        else
        return false;
    }
    
    int main(void)
          {
        cout << "Enter your name: ";
        char *name = new char[20];
        gets(name);
        Player Alex;
        Enemy monster;
        Alex.Wins = 0;
        Alex.Losses=0;
        Alex.HP = 50;
        Alex.Str = 15;
        Alex.Def = 12;
        Alex.SetAge(15);    
        Alex.SetName(name);
        Alex.DisplayStats();
        cout << "\n -------- \n";
        monster.DisplayStats();
        
    
        cout << "Commence Battle!\n";
        
        Battle fight;
        bool DidWin;
        DidWin = fight.DoBattle(Alex, monster);
        cout << (DidWin ? "Congrats you won" : "Uhoh..");
    
             return 0;
          }
    Please help, it's driving me crazy.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  2. #2
    i'm not positive on the scope of your classes, but it might be that the class is considered private so it can't access the HP...

    not entirely sure if that works, but from researching this error, it normally due to trying to access a private variable.

  3. #3
    Paste all of your code. You should be passing by reference into your functions. "user = u;" where is user declared? Also what you did is not needed, user should be in the scope of the function or it should be the parameter u. I think you might have larger problems in your code than just this error.

    Also you are using c++ from the .cpp extension so you have access to std::string. Use it. I mean you included the header at the top and you continually use character pointers.

    off topic:
    -------------------------

    //edit
    this isn't really on topic, but just so you know if you aren't developing for multiple platforms you can use VC++ express 2008. It's completely free and is a lot better than dev-c++ (or I think that's the IDE from looking at the Borland text in the error log.

    Last edited by Sirisian; April 7th, 2008 at 02:58 PM.

  4. #4
    Quote Originally Posted by Sirisian View Post
    Paste all of your code. You should be passing by reference into your functions. "user = u;" where is user declared? Also what you did is not needed, user should be in the scope of the function or it should be the parameter u. I think you might have larger problems in your code than just this error.

    Also you are using c++ from the .cpp extension so you have access to std::string. Use it. I mean you included the header at the top and you continually use character pointers.

    off topic:
    -------------------------

    //edit
    this isn't really on topic, but just so you know if you aren't developing for multiple platforms you can use VC++ express 2008. It's completely free and is a lot better than dev-c++ (or I think that's the IDE from looking at the Borland text in the error log.

    Thanks here's my code:
    Code:
    #include <iostream.h>
    #include <string>
    class Player
      {
      private:
        int age;
        char *name;
      public:
        int SetName(char *n);
        char *Player::GetName();
        int GetAge();
        int SetAge(int a);
        int HP;
        int Str;
        int Def;
        void DisplayStats();
        int Wins;
        int Losses;
      };
    
    class Enemy: Player
    {
        public:
        int age;
        Enemy();
        char *name;
        void DisplayStats();
    };
    
    class Battle
    {
        public:    
        Player user;
        Enemy monster;
        bool DoBattle(Player u, Enemy m);
    };
    
    Player::SetName(char *n)
    {
        name = n;
        return 1;
    }
    char *Player::GetName()
    {
        return name;
    }
    Player::GetAge()
    {
        return age;
    }
    Player::SetAge(int a)
    {
        age = a;
        return a;
    }
    void Player::DisplayStats()
    {
        
        int pad;
        string str = GetName();
        pad = 8 - str.size();
        cout << pad << endl;
        string space;    int i = 0;
        while(i < pad)
        {
        space += " ";
        i++;
        }
        cout << "-----------\n| " << GetName() << space
         << "|\n| Age:" << age <<
         "  |\n| HP: " << HP << "  |\n-----------" 
        << endl;
    }
    
    int RandomNumber(int high, int low)
    {
    return ((rand() % high - low) + low + 1);
    }
    
    Enemy::Enemy()
    {
        age = RandomNumber(100, 10);
        HP = RandomNumber(70, 30);
        Def = (HP / 5) + RandomNumber(5, 0);
        Str = (HP / 5) + RandomNumber(5, 0);
        SetName("Evil Bear");
    }
    void Enemy::DisplayStats()
    {
    cout << "---------\n|" << GetName() << "|\n| Age:" << age << "|\n| HP: " << HP << "|\n---------" << endl;
    }
    bool IsEven(int i)
    {
        if((i % 2) == 0)
            return true;
        else
            return false;
    }
    bool Battle::DoBattle(Player u, Enemy m)
    {
        /*user  =   u;
        monster = m;
        int turn = 0;
        while(user.HP > 0 && monster.HP > 0)
        {
            if(IsEven(turn))
           {
            // User Turn
            monster.HP = monster.HP - u.Str;
           }
        else
           {
            // Monster turn
           }
            
        
        turn++;
        }
        if(user.HP > 0)
        return true;
        else*/
        return false;
    }
    
    
        int main(int argc, char *argv[])
          {
        cout << "Enter your name: ";
        char *name = new char[20];
    
        if(argv[1] != "")
            name = argv[1];
        else
            gets(name);
        Player Alex;
        Enemy monster;
        Alex.Wins = 0;
        Alex.Losses=0;
        Alex.HP = 50;
        Alex.Str = 15;
        Alex.Def = 12;
        Alex.SetAge(15);    
        Alex.SetName(name);
        Alex.DisplayStats();
        cout << "\n -------- \n";
        monster.DisplayStats();
        
    
        cout << "Commence Battle!\n";
        
        Battle fight;
        bool DidWin;
        DidWin = fight.DoBattle(Alex, monster);
        cout << (DidWin ? "Congrats you won" : "You lost.. gutted.");
    
             return 0;
          }

    off topic:
    -------------------------


    I know the VS studio is better, but I'm going back to old school so that I have a better grasp. If I need to do anything graphical I just use C#, I'm only learning C++ because I don't have much else to learn with C#. Cheers anyway.

    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  5. #5
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    class Player
      {
      private:
        int age;
        std::string name;
      public:
        virtual Player::~Player();
        void SetName(std::string n);
        std::string Player::GetName();
        int GetAge();
        void SetAge(int a);
        int HP;
        int Str;
        int Def;
        void DisplayStats();
        int Wins;
        int Losses;
      };
    
    class Enemy: Player
    {
        public:
        int age;
        Enemy();
        std::string name;
        void DisplayStats();
    };
    
    class Battle
    {
        public:
        Player user;
        Enemy monster;
        bool DoBattle(Player &u, Enemy &m);
    };
    
    Player::~Player(){}
    
    void Player::SetName(std::string n)
    {
        name = n;
    }
    std::string Player::GetName()
    {
        return name;
    }
    int Player::GetAge()
    {
        return age;
    }
    void Player::SetAge(int a)
    {
        age = a;
    }
    void Player::DisplayStats()
    {
        int pad;
        std::string str = GetName();
        pad = 8 - str.size();
        std::cout << pad << std::endl;
        std::string space;    int i = 0;
        while(i < pad)
        {
            space += " ";
            i++;
        }
        std::cout << "-----------\n| " << GetName() << space
         << "|\n| Age:" << age <<
         "  |\n| HP: " << HP << "  |\n-----------" 
         << std::endl;
    }
    
    int RandomNumber(int high, int low)
    {
        return ((rand() % high - low) + low + 1);
    }
    
    Enemy::Enemy()
    {
        age = RandomNumber(100, 10);
        HP = RandomNumber(70, 30);
        Def = (HP / 5) + RandomNumber(5, 0);
        Str = (HP / 5) + RandomNumber(5, 0);
        SetName("Evil Bear");
    }
    
    void Enemy::DisplayStats()
    {
         std::cout << "---------\n|" << GetName() << "|\n| Age:" << age << "|\n| HP: " << HP << "|\n---------" << std::endl;
    }
    bool IsEven(int i)
    {
        if((i % 2) == 0)
            return true;
        else
            return false;
    }
    bool Battle::DoBattle(Player &u, Enemy &m)
    {
        /*user  =   u;
        monster = m;
        int turn = 0;
        while(user.HP > 0 && monster.HP > 0)
        {
            if(IsEven(turn))
           {
            // User Turn
            monster.HP = monster.HP - u.Str;
           }
        else
           {
            // Monster turn
           }
            
        
        turn++;
        }
        if(user.HP > 0)
        return true;
        else*/
        return false;
    }
    
    
    int main(int argc, char *argv[])
    {
        Player Alex;
        /*
        if(argv[1] != "")
        {
            Alex.SetName(argv[1]);
        }
        else
        {
            */
            std::cout << "Enter your name: ";
            std::string name;
            std::cin >> name;
            Alex.SetName(name);
        //}
        
        Enemy monster;
        Alex.Wins = 0;
        Alex.Losses=0;
        Alex.HP = 50;
        Alex.Str = 15;
        Alex.Def = 12;
        Alex.SetAge(15);
        Alex.DisplayStats();
        std::cout << "\n -------- \n";
        monster.DisplayStats();
        
    
        std::cout << "Commence Battle!\n";
        
        Battle fight;
        bool DidWin;
        DidWin = fight.DoBattle(Alex, monster);
        std::cout << (DidWin ? "Congrats you won" : "You lost.. gutted.");
        
        std::cout << std::endl;
        system("PAUSE");
        return 0;
    }
    Compiles in Dev-C++ fine

    off topic:
    -------------------------


    when I said use the VC++ IDE I just meant as an IDE and compiler. I don't mean C++.NET. You can created win32 console apps and such. Check it out.


  6. #6
    Thanks very much!
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012