Thread: New to C++ Classes

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    66

    New to C++ Classes

    I am making a program (a text game) and I am using four different classes in it. I using a different file for each class. What I want to know is, how do I run/compile the classes together? If it helps, I am running Dev-C++
    Thanks for the help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Assuming that all the files are part of one project, you would just build the project and Dev-C++ will compile each file and link them together for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    When I want to use the class while in another class, I do: Class classname; then I do classname.method();
    When I do the Class classname; I get an error. What do I need to do?

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Okay, when you have classes A, B, and C for example. Each class needs two files a header file for each, and an implementation file for each, so it will be
    "A.h", "B.h", "C.h" and for implementation "A.cpp", "B.cpp", and "C.cpp"

    In the header file of A, if you would like to use either B or C, you have to put at the top
    Code:
    #include "C.h"
    Also to make sure no multiple inclusion errors occur, put your code in the implementation files in this way

    Code:
    #ifndef A_H_GUARD
    #define A_H_GUARD
    
    //your A.h declarations go here
    
    #endif
    Replacing A_H_GUARD with B_H_GUARD, or C_H_GUARD depending on the file. This way if you want to include C into a and B you don't have to worry about multiple inclusion compile complaints.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, this is what I have as a header:
    Code:
    #include <iostream.h>
    using namespace std;
    #ifndef GameManager_H_GUARD
    #define GameManager_H_GUARD
    
    #include "GameManager.h"
    
    #endif
    I get an error saying "GameManager.h: No such file or directory"
    Yes, there is a GameManager.cpp file.

    Edit: Do I need to have the
    Code:
    #include <iostream.h>
    using namespace std;
    in every class file?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    <iostream.h> is outdated
    <iostream> should be used

    using namespace std; should never occur in the header

    is GameManager.h is located in the same directory as the file that includes it?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Yes, well, the file isn't called GameManager.h it's GameManger.cpp, don't know if that matters or not.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should not include cpp-files... So what exactly are you doing?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I have four classes and I made each one it's own .cpp file and they are all in the same project...

    Another thing, I saw in a tutorial that I need to have #include <string> in the header when using string variables, is this true?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should place them (the class declarations) in header files (.h files in this case), and then implement the classes in .cpp files.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, I made the classes, except the main() class, into .h files.
    What do you mean by implement the clesses in .cpp files?

  12. #12
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    define the functions in the cpp file

    a.h
    Code:
    #ifndef A_H_GUARD
    #define A_H_GUARD
    
    class a
    {
        public:
            a();
        private:
            int i;
    };
    
    #endif
    a.cpp
    Code:
    #include "a.h"
    
    a::a()
    {
        i = 0;
    }

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

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Header (MyClass.h):
    Code:
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    // Any #includes or forward declarations you need go here.
    
    class MyClass
    {
    public:
      MyClass();
      void MemberFunction();
    private:
      int memberVariable;
    };
    
    #endif
    Source (MyClass.cpp):
    Code:
    #include "MyClass.h"
    // Any other #includes you need go here.
    
    MyClass::MyClass() : memberVariable(0)
    {
    }
    
    void MyClass::MemberFunction()
    {
      // Do stuff
    }
    Main (main.cpp):
    Code:
    // Other #includes you need can go here.
    #include "MyClass.h"
    // Other #includes you need can go here.
    
    int main()
    {
      MyClass myVariable;
      myVariable.MemberFunction();
    }
    If you have a second class that uses the first class, then the cpp file should #include both its own header and the other class's header.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For example:
    Code:
    #ifndef X_H
    #define X_H
    
    // x.h
    
    class X
    {
    public:
        void foo();
    };
    
    #endif
    Code:
    // x.cpp
    
    #include "x.h"
    
    void X::foo()
    {
        // ...
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK I'll do that. What about the main class? I can't really do that sort of thing since there's no constructor.

    Edit: I'm still getting an error at the
    Code:
    #include "GameManager.h"
    line
    Last edited by Cypher; 06-20-2007 at 12:33 PM.

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