Thread: New to C++ Classes

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No header is necessary for the main file that holds the main function. That's what you're talking about, right? See my example of main.cpp above.

    >> Edit: I'm still getting an error at the #include "GameManager.h"
    You also realize that you typed GameManger in at least one spot in this thread. Make sure your spellings are accurate.
    Last edited by Daved; 06-20-2007 at 12:52 PM.

  2. #17
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    what's the error message?
    is the file name correct?

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #18

  4. #19
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    can't you post the code here? i have to wait 45 seconds for each dl on megaupload

    edit: or better yet, post your error message

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  5. #20
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    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 robo.cpp -o robo.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"

    In file included from ShopClass.h:4,
    from GameManager.h:6,
    from robo.cpp:2:
    GameManager.h:1:1: unterminated #ifndef
    In file included from robo.cpp:2:
    GameManager.h:1:1: unterminated #ifndef
    robo.cpp:9: error: `string' does not name a type
    robo.cpp: In function `int main()':
    robo.cpp:16: error: `cout' undeclared (first use this function)
    robo.cpp:16: error: (Each undeclared identifier is reported only once for each function it appears in.)

    make.exe: *** [robo.o] Error 1

    Execution terminated

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> unterminated #ifndef
    That means that you are missing the #endif at the bottom, or you have a typo. Post the GameManager.h file, or at least the top and bottom lines.

    >> error: `string' does not name a type
    You have to #include <string> and either add using namespace std or type std::string.

    >> error: `cout' undeclared (first use this function)
    You have to #include <iostream> and either add using namespace std or type std::cout.

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cypher View Post
    OK I'll do that. What about the main class? I can't really do that sort of thing since there's no constructor.
    That's because main() is not a class.

  8. #23
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Quote Originally Posted by brewbuck View Post
    That's because main() is not a class.
    lol, OK. I'm coming off of Java here so I need to get into the C++ mind set.
    GameManager.h
    Code:
    #ifndef ShopClass_H_GUARD
    #define ShopClass_H_GUARD
    #ifndef BattleClass_H_GUARD
    #define BattleClass_H_GUARD
    
    #include "ShopClass.h"
    #include "BattleClass.h"
    
    #endif
    
    
    class GameManager
    {
          public:
                 void Manager();
    };

  9. #24
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    in GameManager.h, put the following header guards
    Code:
    #ifndef GAMEMANAGER_H_GUARD
    #define GAMEMANAGER_H_GUARD
    and at the end of the file, put
    Code:
    #endif
    The header guards are to prevent the header file from begin included twice and prevent getting an error of a class being redeclared

    so you would have...
    Code:
    #ifndef GAMEMANAGER_H_GUARD
    #define GAMEMANAGER_GUARD
    
    #include "ShopClass.h"
    #include "BattleClass.h"
    
    class GameManager
    {
          public:
                 void Manager();
    };
    
    #endif

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  10. #25
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, thanks a lot.
    Now, how do I use a variable from another class?
    This is what I did but I know that it's wrong.

    GameManager.h

    Code:
    #ifndef Gamemanager_H_GUARD
    #define GameManager_H_GUARD
    
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    using namespace std;
    
    class GameManager
    {
          public:
                 void Manager();
                 int money = 200;
                 int lives = 3;
                 int attack = 0;
                 int defense = 0;
                 string ShopBattle;
                 int Battle;
                 int Shop;
    };
    #endif
    GameManager.cpp

    Code:
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    
    
    void GameManager::Manager()
    {
         int money = 200;
         int lives = 3;
         int attack = 0;
         int defense = 0;
         string ShopBattle;
         int Battle;
         int Shop;
         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") {
                        shop.Shop();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                battle.Battle();
         } else {
                cout << "Error: Invalid Input";
         }
    }

  11. #26
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    you didn't include "GameManager.h" in your "GameManager.cpp" file

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  12. #27
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, but am I doing the whole variable thing right? Now can I just do "manager.attack = 3"?

    These are my errors now:
    Code:
    make.exe -f "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win" all
    g++.exe -c robo.cpp -o robo.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"   
    
    In file included from BattleClass.h:4,
                     from GameManager.h:5,
    
                     from ShopClass.h:4,
                     from GameManager.h:4,
                     from robo.cpp:4:
    GameManager.h:13: error: ISO C++ forbids initialization of member `money'
    GameManager.h:13: error: making `money' static
    GameManager.h:13: error: ISO C++ forbids in-class initialization of non-const static member `money'
    GameManager.h:14: error: ISO C++ forbids initialization of member `lives'
    GameManager.h:14: error: making `lives' static
    GameManager.h:14: error: ISO C++ forbids in-class initialization of non-const static member `lives'
    GameManager.h:15: error: ISO C++ forbids initialization of member `attack'
    GameManager.h:15: error: making `attack' static
    GameManager.h:15: error: ISO C++ forbids in-class initialization of non-const static member `attack'
    GameManager.h:16: error: ISO C++ forbids initialization of member `defense'
    GameManager.h:16: error: making `defense' static
    GameManager.h:16: error: ISO C++ forbids in-class initialization of non-const static member `defense'
    In file included from ShopClass.h:4,
    
                     from GameManager.h:4,
                     from robo.cpp:4:
    GameManager.h:10: error: redefinition of `class GameManager'
    GameManager.h:10: error: previous definition of `class GameManager'
    
    In file included from robo.cpp:4:
    GameManager.h:10: error: redefinition of `class GameManager'
    GameManager.h:10: error: previous definition of `class GameManager'
    
    make.exe: *** [robo.o] Error 1
    
    Execution terminated
    Last edited by Cypher; 06-20-2007 at 02:34 PM.

  13. #28
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Everything is being redeclared here
    Code:
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    
    
    void GameManager::Manager()
    {
         int money = 200;
         int lives = 3;
         int attack = 0;
         int defense = 0;
         string ShopBattle;
         int Battle;
         int Shop;
         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") {
                        shop.Shop();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                battle.Battle();
         } else {
                cout << "Error: Invalid Input";
         }
    }
    Code:
                     from GameManager.h:4,
                     from robo.cpp:4:
    GameManager.h:10: error: redefinition of `class GameManager'
    GameManager.h:10: error: previous definition of `class GameManager'
    
    In file included from robo.cpp:4:
    GameManager.h:10: error: redefinition of `class GameManager'
    GameManager.h:10: error: previous definition of `class GameManager'
    These are the kinds of errors header guards are supposed to prevent
    Since you're getting them, it means that you didn't implement your header guards properly

    Code:
    #ifndef GameManager_H_GUARD
    #define GameManager_H_GUARD
    C++ is case sensitive
    Last edited by h_howee; 06-20-2007 at 04:32 PM.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  14. #29
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Ok, now I'm getting errors with the main class
    Code:
    #include <iostream>
    #include <string>
    #include "GameManager.h"
    using namespace std;
    
    int main()
    {
        GameManager manager;
        cout << "::Welcome to Robo Madness::\n\n";
        manager.Manager();
        
        return 0;
    }
    and the errors:
    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 robo.cpp -o robo.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"   
    
    In file included from robo.cpp:4:
    GameManager.h:13: error: ISO C++ forbids initialization of member `money'
    GameManager.h:13: error: making `money' static
    GameManager.h:13: error: ISO C++ forbids in-class initialization of non-const static member `money'
    GameManager.h:14: error: ISO C++ forbids initialization of member `lives'
    GameManager.h:14: error: making `lives' static
    GameManager.h:14: error: ISO C++ forbids in-class initialization of non-const static member `lives'
    GameManager.h:15: error: ISO C++ forbids initialization of member `attack'
    GameManager.h:15: error: making `attack' static
    
    GameManager.h:15: error: ISO C++ forbids in-class initialization of non-const static member `attack'
    GameManager.h:16: error: ISO C++ forbids initialization of member `defense'
    GameManager.h:16: error: making `defense' static
    GameManager.h:16: error: ISO C++ forbids in-class initialization of non-const static member `defense'
    
    make.exe: *** [robo.o] Error 1
    
    Execution terminated
    I thought that you didn't need to make a .h file for the main method. So, what do I do if I want to use a class in the main method?
    Last edited by Cypher; 06-20-2007 at 07:11 PM.

  15. #30
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    I dont know how you set up your other files...but making everything in GameManager class public is a no no.

    GameManager.h

    Code:
    #ifndef GAMEMANAGER_H
    #define GAMEMANAGER_H
    #include "ShopClass"
    #include "BattleClass"
    #include <iostream>
    #include <string>
    
    class GAMEMANAGER
    {
    private:
           std::string ShopBattle;
           int money;
           int lives;
           int attack;
           int defense;
           int battle;
           int shop;
    // Now you access these variables by the class methods aka functions
    
    public:
          GAMEMANAGER();
          void SetShopBattle(std::string newShopBattle);
          void SetMoney(int newmoney);
          void SetLives(int newlives);
          void SetAttack(int newattack);
          void SetDefense(int newdefense);
          void SetBattle(int newbattle);
          void SetShop(int newshop);
          std::string GetShopBattle();
          int GetMoney();
          int GetLives();
          int GetAttack();
          int GetDefense();
          int GetBattle();
          int GetShop();
    };
    
    #endif

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