Thread: New to C++ Classes

  1. #106
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Here is my GameManager.cpp file:
    Code:
    #include "GameManager.h"
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}
    
    void GameManager::MainManager()
    {
         system("CLS");
         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;
         system("CLS");
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                        Shop.ShopMain();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                Battle.BattleMain();
         } else {
                cout << "Error: Invalid Input";
         }
    }
    int GameManager::GetAttack()
    {
        return attack;
    }
    int GameManager::GetDefense()
    {
        return defense;
    }
    int GameManager::GetLives()
    {
        return lives;
    }
    void GameManager::Lives()
    {
         lives -= 1;
    }
    int GameManager::GetMoney()
    {
        return money;
    }
    void GameManager::AddAttack(int amount)
    {
         attack += amount;
    }
    
    void GameManager::AddDefense(int amount)
    {
         defense += amount;
    }
    void GameManager::SetMoney(int newmoney)
    {
         money = newmoney;
    }
    
    void GameManager::SpendMoney(int amount)
    {
        SetMoney(money - amount);
    }
    
    void GameManager::AddMoney(int amount)
    {
         SetMoney(money + amount);
    }
    But seriously, try the game this time. You will understand what I'm trying to tell you about the variables resetting themselves.

    Oh, I put in a few cool little things I learned to make the game look cooler.

    http://z33.zupload.com/download.php?...filepath=38139

  2. #107
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You shouldn't have the game logic in the constructor. The constructor's purpose is to initialize the object.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #108
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Initialize the object? That's kind of like doing something like this?
    Code:
    GameManager manager;
    manager.SomeFunction(initialize a variable, another variable);
    So, if that's what it is, then what do I need to do?
    I don't really understand what you're trying to say with that.

  4. #109
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You shouldn't have the game logic in the constructor. The constructor's purpose is to initialize the object.
    In the posted code the logic is not in the constructor, it's in the MainManager function.

    >> But seriously, try the game this time. You will understand what I'm trying to tell you about the variables resetting themselves.
    I honestly don't even have time to download the source files above, let alone try the game. Sorry.

    You said, "I'm still getting that problem where every time the program goes back to the MainManager, it must run the constructor and it sees the variables there and it sets the variables to what they are declared in in the constructor." Then you posted the GameManager class that I already said looks fine and is not calling any constructors out of place. I'm going to be offline for awhile anyway, but the only I could've helped is to see the specific snippet of code you were talking about.

    Good luck.

  5. #110
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    The only place where I call the MainManager function is at the end of the shop and battle classes. They just do manager.MainManager();

    There's really nothing to show. I just think that I'm putting the variables in the wrong place or something.

  6. #111
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    ..and the rest.

  7. #112
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok, so I looked at your BattleClass BattleMain and I see your problem. It is the exact same problem, you create a local GameManager object, which is different than the GameManager that you start with.

    The solution is a little trickier because your GameManager knows about the BattleClass and the BattleClass knows about the GameManager. This makes things trickier than with just one way.

    The best solution for now is to pass an argument to BattleMain. Have BattleMain take a reference to the GameManager:
    Code:
    void BattleClass::BattleMain(GameManager& manager)
    Then remove the GameManager object that you declared inside that function and use the parameter instead. Make sure to change the function declaration and definition. Do the same for ShopClass.

  8. #113
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I think I might have done something wrong when I did that.

    The error
    Code:
    ShopClass.h:12: error: variable or field `ShopMain' declared void
    ShopClass.h:12: error: expected `;' before '(' token
    
    In file included from GameManager.h:5,
                     from BattleClass.cpp:1:
    BattleClass.h:12: error: variable or field `BattleMain' declared void
    BattleClass.h:12: error: expected `;' before '(' token
    BattleClass.cpp:9: error: no `void BattleClass::BattleMain(GameManager&)' member function declared in class `BattleClass'
    The BattleMain function now looks like this:
    Code:
    void BattleClass::BattleMain(GameManager& manager)
    ...and the same with the ShopMain function accordingly.

    This is how I declared the function:
    Code:
    public:
                 void BattleMain(GameManager& manager);
    Do I need to call it differently or can I just leave it the way I was doing it before.
    Code:
    manager.MainManager();

  9. #114
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Input from anyone would be greatly appreciated.

  10. #115
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Sorry, I confused GameManager and MainManager. Those aren't very good names.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #116
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Add a forward declaration of GameManager to your BattleClass (and ShopClass) header file. It looks like this:
    Code:
    class GameManager;
    and goes before your BattleClass class code.

    >> manager.MainManager();
    Don't call this inside BattleMain or ShopMain. Let the functions return on their own. Then add a loop to MainManager() so that it runs over and over.

  12. #117
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I think I did what you said correctly, I'm getting more errors though that I don't know how to fix.
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win"
    Executing  make...
    make.exe -f "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win" all
    g++.exe -c GameManager.cpp -o GameManager.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   
    
    GameManager.cpp: In member function `void GameManager::MainManager()':
    GameManager.cpp:27: error: no matching function for call to `ShopClass::ShopMain()'
    
    ShopClass.h:14: note: candidates are: void ShopClass::ShopMain(GameManager&)
    GameManager.cpp:29: error: no matching function for call to `BattleClass::BattleMain()'
    BattleClass.h:14: note: candidates are: void BattleClass::BattleMain(GameManager&)
    
    make.exe: *** [GameManager.o] Error 1
    
    Execution terminated

  13. #118
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't forget to pass the GameManager to those functions when you call them. That's the whole point. Since you are calling ShopMain and BattleMain from inside your GameManager, you have to pass *this as the argument.

  14. #119
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, what I'm going to try and do is finish up this game and then do my best to learn more about classes in C++. I really don't understand what you mean by "pass it" and I did try to Google it but I'm not really sure what exactly I would Google.

    This is the code for GameManager::MainManager:
    Code:
    void GameManager::MainManager()
    {
         while (true)
         {
         system("CLS");
         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;
         
         system("CLS");
         
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                Shop.ShopMain(GameManager& manager);
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                Battle.BattleMain(GameManager& manager);
         } else {
                cout << "\nError: Invalid Input\n\n";
                cout << "The Program Will Now Exit\n\n";
                system("PAUSE");
         }
         }
    }
    .. and this is how I declared the shop and battle methods:
    Code:
    void ShopClass::ShopMain(GameManager& manager)
    and when I initialized the methods in the header file, I did:
    Code:
    public:
                 void ShopMain(GameManager& manager);
    and I did put
    Code:
    class GameManager;
    above the two classes (shop and battle) in the header files.


    This is the error I'm getting:
    Code:
    GameManager.cpp: In member function `void GameManager::MainManager()':
    GameManager.cpp:27: error: expected primary-expression before '&' token
    GameManager.cpp:27: error: `manager' undeclared (first use this function)
    GameManager.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
    GameManager.cpp:29: error: expected primary-expression before '&' token

  15. #120
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Shop.ShopMain(GameManager& manager);
    You're close, but that's not how you pass something to a function. You have to pass *this.

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, 05: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