New to C++ Classes

This is a discussion on New to C++ Classes within the C++ Programming forums, part of the General Programming Boards category; It should be Code: attack += amount; The same for all the other places you have =+, they should be ...

  1. #61
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It should be
    Code:
    attack += amount;
    The same for all the other places you have =+, they should be +=. All your =- should be -=. If attack = attack + amount doesn't work then there are probably other problems with your code that this won't fix.


    BTW, I guess I see why it compiles.
    Code:
    attack =+ amount;
    is the same as
    Code:
    attack = +amount;
    which is the same as
    Code:
    attack = amount;
    Obviously that's not what you want.
    Last edited by Daved; 06-21-2007 at 08:18 PM.

  2. #62
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I made a few changes and I'm getting

    GameManager.h:28: error: `int GameManager::GetMoney()' and `int GameManager::GetMoney()' cannot be overloaded

    Tried to look it up, don't know what it means.


    Edit: Oops, never mind that.
    Last edited by Cypher; 06-21-2007 at 08:23 PM.

  3. #63
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    It's still not doing what I want it to. It wont add money that you win, it wont subtract money when you buy stuff, you can buy anything no matter how much money you have and you can beat any robot. Why wont the functions work?

  4. #64
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Read Daved's post.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #65
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    void GameManager::Manager()
    Are you trying to write a constructor? You should really read about its syntax
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #66
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I replaced "void GameManager::Manager()" with "GameManager::GameManager()"
    I don't know if I need a deconstructor or not. If I do, what do I put in it, all of the variables?

    I'm also getting this error: robo.cpp:12: error: invalid use of `class GameManager'

  7. #67
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    If your class manages the memory of any of its member variables, then you need to write a destructor to ensure that the memory is deallocated correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #68
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    When I call on the constructor from a different class do I just type "GameManager();"?


    I don't think I did the constructor and destructor correctly
    Code:
    GameManager::GameManager()
    {
         int money = 200;
         int lives = 3;
         int attack = 0;
         int defense = 0;
         ShopClass shop;
         BattleClass battle;
         cout << "Current Status:\n";
         cout << "Money: $" << money << "\n";
         cout << "Lives: " << lives << "\n";
         cout << "Attack Points: " << attack << "\n";
         cout << "Defense Points: " << defense << "\n";
         cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
         cin >> ShopBattle;
         
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                        ShopClass();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                BattleClass();
         } else {
                cout << "Error: Invalid Input";
         }
    }
    GameManager::~GameManager()
    {
     delete money;
     delete lives;
     delete attack;
     delete defense;
    }
    Last edited by Cypher; 06-22-2007 at 05:27 AM.

  9. #69
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    You created local variables named money, lives, attack and defense in your constructor. As stated, you should use an initialisation list to initialise those member variables instead:
    Code:
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0)
    {
        ShopClass shop;
        BattleClass battle;
        // ...
    Since all four member variables are of a primitive type, you do not need a destructor. To write delete money when you did not write new money is a mistake.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #70
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I don't understand when I'm supposed to do that and when I'm not.

  11. #71
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Read about constructors and destructors.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #72
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Code:
    void GameManager::AddMoney(int amount)
    {
         money =+ amount;
         if (money < 0)
              money = 0;
    }
    I dont know if this is the right way...but this is how I would do it:
    I would seperate them into two functions first...

    Code:
    void GAMEMANAGER::SetMoney(int newmoney)
    {
         money = newmoney;
         if (money < 0)
              money = 0;
    }
    
    void GAMEMANAGER::SpendMoney(int amount)
    {
        SetMoney(money - amount);
    }
    
    void GAMEMANAGER::AddMoney(int amount)
    {
         SetMoney(money + amount);
    }

  13. #73
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, I took a break from my program for a little while and I came back to it and tried to make any necessary changes to it but when I run it then window opens and then closes again. I don't know if I am calling the GameManager's constructor wrong or something in the main() function. I do "GameManager();" when I want to run the constructor for GameManager.cpp.
    This is the main() function
    Code:
    int main()
    {
        cout << "::Welcome to Robo Madness::\n\n";
        GameManager();
        return 0;
    }
    and the GameManager.cpp constructor
    Code:
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0)
    {
         ShopClass shop;
         BattleClass battle;
         cout << "Current Status:\n";
         cout << "Money: $" << money << "\n";
         cout << "Lives: " << lives << "\n";
         cout << "Attack Points: " << attack << "\n";
         cout << "Defense Points: " << defense << "\n";
         cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
         cin >> ShopBattle;
         
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                        ShopClass();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                BattleClass();
         } else {
                cout << "Error: Invalid Input";
                GameManager();
         }
    }
    I'm not sure if I'm supposed to put all of that code that's in the constructor in it's own function or not.

  14. #74
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Can anyone help?

  15. #75
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
         ShopClass shop;
         BattleClass battle;
    These are local to the GameManager constructor. When the constructor finishes, you won't have the shop or battle anymore.

    Code:
        ShopClass();
        BattleClass();
        GameManager();
    This is not a way to call the constructor. Constructor is used to construct an instance. Do you see an instance here?

    Really, you are trying to write fairly complicated classes, yet you barely understand what a constructor is, what an instance is etc. May-be you should read some tutorials and practice with simpler exercises first.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

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. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21