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; hmm... it looks like you are trying to use classes by treating the class name as the name of a ...

  1. #76
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    hmm... it looks like you are trying to use classes by treating the class name as the name of a function (the constructor), and then attempting to do procedural programming.

    What you should do is to determine what you want to model. What is a GameManager? What is it for, and what does it do? Answering this would allow you to design its member functions, especially its public interface. From there you can determine what it must keep track of, and thus decide on what its member variables should be.

    Instead of invoking the constructor by treating it as no more than a function call, you would create an object, and use that object. The constructor is just to initialise the state of that object.
    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

  2. #77
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    So do I just use the constructor to initialize variables and then make a new function and put all the code that I have in the constructor right now in it?

  3. #78
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    So do I just use the constructor to initialize variables and then make a new function and put all the code that I have in the constructor right now in it?
    Most likely yes.
    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

  4. #79
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I keep coming back to my program and I keep having trouble.

    BattleClass.cpp: In member function `void BattleClass::BattleMain()':
    BattleClass.cpp:11: error: no matching function for call to `GameManager::GameManager()'
    GameManager.h:10: note: candidates are: GameManager::GameManager(const GameManager&)
    GameManager.h:22: note: GameManager::GameManager(int, int, int, int)

  5. #80
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    It looks like you do not have a default constructor for GameManager, but there is an attempt to create a GameManager object via the default constructor in BattleClass::BattleMain().
    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

  6. #81
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, I see what I did.

    Now I'm getting this
    GameManager.cpp:8: error: definition of implicitly-declared `GameManager::GameManager()'

    On the constructor. I don't know if I should have the variables declared in this way or if I should have them declared in the MainManager method.

    Code:
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}

  7. #82
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Fixed it, new errors though. I post the errors here and then I try my best to figure them out, but I would like to have someone post some help too.

    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"   
    
    g++.exe robo.o BattleClass.o ShopClass.o GameManager.o  -o "RoboMadness.exe" -L"C:/Dev-Cpp/lib"  
    
    GameManager.o(.text+0x216):GameManager.cpp: multiple definition of `GameManager::GameManager()'
    BattleClass.o(.text$_ZN11GameManagerC1Ev[GameManager::GameManager()]+0x0):BattleClass.cpp: first defined here
    GameManager.o(.text+0x339):GameManager.cpp: undefined reference to `ShopClass::ShopClass()'
    
    GameManager.o(.text+0x344):GameManager.cpp: undefined reference to `BattleClass::BattleClass()'
    collect2: ld returned 1 exit status
    
    make.exe: *** [RoboMadness.exe] Error 1
    
    Execution terminated
    Last edited by Cypher; 07-02-2007 at 04:31 PM.

  8. #83
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Let me guess. You put out-of-line definitions of constructors in header files.

    Make them inline or put them in source files.
    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

  9. #84
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    This is how I have the constructor:
    Code:
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}
    I don't know if I have to declare the variables differently than I did. When I declared the constructor in the .h file, I have:
    Code:
    GameManager();
    I don't know if that's what you meant. If not, then what do you mean by out of line and inline?
    I tried to Google it.

  10. #85
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    The compiler will create a binary object file from a source file. The idea is to let the header file be a sort of catalogue for your class/library for the program to use. So, in your case (I guess: I haven't read the entire thread)
    Code:
    //GameManager.h
    #ifndef GAMEMANAGER_H
    #define GAMEMANAGER_H
    
    struct GameManager
    {
          GameManager(); //Just the name of the function, and its arguments.
    };
    
    //don't forget the endif
    #endif
    Then...
    Code:
    //GameManager.cpp
    #include "GameManager.h"
    //This is where your compiler is going to look, after seeing your stuff declared in the .h
    
    GameManager::GameManager() : money(200) /*initializers, etc...*/
    {
          //empty
    }
    Now, source files that want to use GameManager will include GameManager.h . For each function call regarding GameManager (unless your compiler recognizes inline possibilities), the system will jump to the definition in your program file, and run it, and then go back. If, instead, you want the function code to be reiterated (expanded on spot) where it is called, you can make a request to the compiler to do so with inline.
    Say you had a function GameManager::small_task(). You would declare it as you have been:
    Code:
    //GameManager.h
    #ifndef GAMEMANAGER_H
    #define GAMEMANAGER_H
    
    struct GameManager
    {
          GameManager();
          void small_task(); //Note how I still am not writing the function code here
    };
    
    //don't forget the endif
    #endif
    And then
    Code:
    //GameManager.cpp
    #include "GameManager.h"
    
    inline void GameManager::small_task()
    {
          std::cout << "My! This is such a small task. I wish I could avoid the overhead of a function call...." << std::endl;
    }
    
    GameManager::GameManager() : money(200) /*initializers, etc...*/
    {
          //empty
    }
    So that's how that works. Now, if you go ahead and write the definition (body code) of the function in the header file, then that is equivalent to doing the inline I just did. The method involving inline, however, is often preferred.
    Last edited by CodeMonkey; 07-02-2007 at 09:42 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #86
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    So if a function is going to be called more than once, I need the inline command...

    I am getting linker errors now.

  12. #87
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, that's not the criteria. You use inline if you think that inlining the function would be a good thing.

    The point is: if you use inline, the implementation must be in the header, but if you do not use inline, the implementation must be in a normal source file. (Unless it's a template. That's different again.)
    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

  13. #88
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, I wasn't using inline before so I didn't need the implementation in the header, so that wasn't the problem.

    I took them out and the program ran now. I don't understand why it didn't run before...
    It seems to work, however, the variables seem to be resetting themselves according to the last action taken. i.e. I'll buy some armour and the defense/money variables will go up/down accordingly. Then, I will battle a robot and I will lose, like I should, but then the money is back at $200, the amount you start with, and the defense is at 0 but the lives is at 2, one less. What do I need to do with the variables to make this stop?

  14. #89
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    bump, I could really use the help on this one.

  15. #90
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Well....it just seems like you are setting some variables in places you shouldn't be doing so...show us your code.
    My Website

    "Circular logic is good because it is."

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