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; GameManager.cpp Code: #include GameManager.h GAMEMANAGER::GAMEMANANGER() { SetMoney(200); SetLives(3); SetAttack(0); SetDefense(0); SetBattle(0); SetShop(0); } void GAMEMANAGER::SetShopBattle(std::string newShopBattle) { ShopBattle = newShopBattle; ...

  1. #31
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    GameManager.cpp

    Code:
    #include GameManager.h
    
    GAMEMANAGER::GAMEMANANGER()
    {
          SetMoney(200);
          SetLives(3);
          SetAttack(0);
          SetDefense(0);
          SetBattle(0);
          SetShop(0);
    
    }
    
    void GAMEMANAGER::SetShopBattle(std::string newShopBattle)
    {
         ShopBattle = newShopBattle;
    }
    
    void GAMEMANAGER::SetMoney(int newmoney)
    {
         money = newmoney;
         if (money < 0)
              money = 0; // You cant have a negative amount of money ;)
    }
    
    void GAMEMANAGER::SetLives(int newlives)
    {
         lives = newlives;
    }
    
    void GAMEMANAGER::SetAttack(int newattack)
    {
         attack = newattack;
    }
    
    void GAMEMANAGER::SetDefense(int newdefense)
    {
         defense = newdefense;
    }
    
    void GAMEMANAGER::SetBattle(int newbattle)
    {
         battle = newbattle;
    }
    // this is all just redundant code...but it should kinda give you an idea how classes are implemented in c++

  2. #32
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, that's sort of what I did when I made the program in Java, so I guess I'll do it again. What would the "Get..." variables do?

    If I have those variables as private how can I access them in the battle class. This is the sort of thing I need to do:
    Code:
    if (PlayersAttack >= 1(which is the robot's attack)) {
    he wins
    }
    This is what I made my GameManager.h into:
    Code:
    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:
          Manager();
          void AddMoney(int newmoney);
          void Lives(int newlives);
          void AddAttack(int newattack);
          void AddDefense(int newdefense);
    };
    And my GameManager.cpp:
    Code:
    GameManager::Manager()
    {
    
    }
    
    void GameManager::AddMoney(int amount)
    {
         money =+ amount;
         if (money < 0)
              money = 0;
    }
    
    void GameManager::Lives()
    {
         lives =- 1;
    }
    
    void GameManager::AddAttack(int amount)
    {
         attack =+ amount;
    }
    
    void GameManager::AddDefense(int amount)
    {
         defense =+ amount;
    }
    
         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";
         }
    }
    Last edited by Cypher; 06-21-2007 at 06:36 AM.

  3. #33
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    What would the "Get..." variables do?
    It's a function, not a variable

    When you want to use say the money money variable to display or or to do some computing with it, you can't just get the value with "manager.money" since money is private. You'll have to make a function that returns the value of money
    Code:
    class GameManager
    {
        private:
            int money;
            //...declare other variables
        public:
            int GetMoney();
            //...declare other functions
    };
    Code:
    int GameManager::GetMoney()
    {
        return money;
    }
    so instead of directly getting the value with "manager.money", you would call "manager.GetMoney()"

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

  4. #34
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Ohhhhhhhhhhhh
    Somebody tried explaining that before but you explained it so much better.

    Now, I get this
    Code:
    robo.cpp: In function `int main()':
    robo.cpp:8: error: `GameManager' undeclared (first use this function)
    robo.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)
    robo.cpp:8: error: expected `;' before "manager"
    
    robo.cpp:10: error: `manager' undeclared (first use this function)
    Last edited by Cypher; 06-21-2007 at 01:12 PM.

  5. #35
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I fixed some of the errors but I'm getting this one in the robo.cpp file with the main() class.
    GameManager.h:21: error: ISO C++ forbids declaration of `Manager' with no type

    I don't know what it is but I do have this: GameManager manager; and I have the #include "GameManager.h" in the header.

    Also, what is the main() called? Class or function or what?

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    In your header you declare a class called GAMEMANAGER, but then in your other code you use a class called GameManager.

    Those are two different names, case matters in C++.

  7. #37
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Yeah, I fixed that before the error.

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Are you sure you fixed it all the way? That is what would cause that error, if the spellings or case doesn't match.

    >> Also, what is the main() called? Class or function or what?
    main is a function like any other function. It is just special in that all programs must have a function called main in global scope and it must have int as its return type and there are some restrictions on the types of parameters it must take. However, it still works pretty much like any other function.

  9. #39
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, I am still having a butt load of errors: (I fixed the last one)
    Code:
    BattleClass.h:12: error: declaration of `void BattleClass::Battle()'
    BattleClass.h:10: error: conflicts with previous declaration `int BattleClass::Battle'
    
    BattleClass.cpp: In member function `void BattleClass::Battle()':
    BattleClass.cpp:18: error: no match for 'operator>>' in 'std::cin >> ((BattleClass*)this)->BattleClass::Battle'
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:87: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:93: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:102: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:111: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:133: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:164: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:186: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:217: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:239: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:261: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:284: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
    
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:306: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:329: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:351: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:373: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
    
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:395: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:417: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/istream:646: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&) [with _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/istream:651: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&) [with _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/istream:687: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>]
    C:/Dev-Cpp/include/c++/3.4.2/istream:692: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>]
    BattleClass.cpp:19: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:20: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:25: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:27: error: invalid use of member (did you forget the `&' ?)
    
    BattleClass.cpp:28: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:28: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:33: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:35: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:36: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:36: error: invalid use of member (did you forget the `&' ?)
    
    BattleClass.cpp:41: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:43: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:44: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:44: error: invalid use of member (did you forget the `&' ?)
    
    BattleClass.cpp:49: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:51: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:52: error: invalid use of member (did you forget the `&' ?)
    BattleClass.cpp:52: error: invalid use of member (did you forget the `&' ?)
    
    BattleClass.cpp:57: error: invalid use of member (did you forget the `&' ?)
    It looks very redundant so it must be something that I need to learn to do.

  10. #40
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    ...and my BattleClass.cpp file:
    Code:
    #include "GameManager.h"
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    using namespace std;
    
    
    void BattleClass::Battle()
    {
         GameManager manager;
         cout << "Welcome to the Battle Arena\n";
         cout << "Who would you like to battle?\n";
         cout << "1. Robot 1\n";
         cout << "2. Robot 2\n";
         cout << "3. Robot 3\n";
         cout << "4. Robot 4\n";
         cout << "5. Robot 5\n";
         cin >> Battle;
         if (Battle == 1){
            if (manager.GetAttack >= 1){
               cout << "Attack Successful\n You Won $200\n";
               manager.AddMoney(200);           
            } else {
              cout << "You Lost\n";
              manager.GetLives =- 1;
            }
         } else if (Battle == 2){
           if (manager.GetAttack >= 2 && manager.GetDefense >= 1){
              cout << "Attack Successful\n You Won $300\n";
              manager.AddMoney(300);           
           } else {
             cout << "You Lost\n";
             manager.GetLives =- 1;
           }
         } else if (Battle == 3){
           if (manager.GetAttack >= 3 && manager.GetDefense >= 2){
              cout << "Attack Successful\n You Won $400\n";
              manager.AddMoney(400);           
           } else {
             cout << "You Lost\n";
             manager.GetLives =- 1;
           }
         } else if (Battle == 4){
           if (manager.GetAttack >= 4 && manager.GetDefense >= 3){
              cout << "Attack Successful\n You Won $500\n";
              manager.AddMoney(500);           
              } else {
                cout << "You Lost\n";
                manager.GetLives =- 1;
              }
           } else if (Battle == 5){
             if (manager.GetAttack >= 5 && manager.GetDefense >= 4){
                cout << "Attack Successful\n You Won $600\n";
                manager.AddMoney(600);           
             } else {
               cout << "You Lost\n";
               manager.GetLives =- 1;
             }
           } else {
             cout << "Error: Invalid Input\n";
           }
           manager.Manager();
    }

  11. #41
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    When you get a "butt load of errors", always start with the first one, since often fixing that will fix a bunch below it.

    In this case, the error message really explains the problem. It points at line 18, which is this line:
    Code:
    cin >> Battle;
    Then it says:
    Code:
    error: no match for 'operator>>' in 'std::cin >> ((BattleClass*)this)->BattleClass::Battle'
    This means that it can't find an operator>> that works with Battle.

    What is Battle? Looking at your code, it is the name of a function. You have an int member variable in there called battle, is that what you wanted? Remember, case matters in C++.

  12. #42
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Battle is just the number Robot that he player wants to battle against.
    I have the integer in the BattleClass.h file:
    class BattleClass
    {
    private:
    int Battle;

  13. #43
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You mean you have both a member function and a member variable named Battle inside the BattleClass? The code you posted with the error was a member function of BattleClass called Battle. Now you say you have a member variable named Battle. You can't have both with the same name.

    Programming is detail oriented, especially C++. You've got to pay attention to the details like spelling, case, etc. You've got to pay attention to the details when figuring out your errors and when giving information about them to those trying to help. You'll be a much better programmer if you work on that.

  14. #44
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK thanks, I'll try to do that.
    So, what do these mean, because I'm still getting them:
    invalid use of member (did you forget the `&' ?)

    Nevermind, I'm working on it for now. I'm actually finding a lot of my own errors now. They're just little mistakes.
    Last edited by Cypher; 06-21-2007 at 02:35 PM.

  15. #45
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I don't know what to do for this one:
    if (restart == "y" || restart == "Y")
    is what I have and I'm getting the error:
    ISO C++ forbids comparison between pointer and integer

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, 11:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  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, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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