Probably A Stupid Question

This is a discussion on Probably A Stupid Question within the C++ Programming forums, part of the General Programming Boards category; One thing I've never got my head around is multiple file projects. For example: Code: // myclass.h class myClass { ...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    Probably A Stupid Question

    One thing I've never got my head around is multiple file projects. For example:

    Code:
    // myclass.h
    
    class myClass
    {
    public:
       myClass (void);
    
    private:
       int x;
    };
    Code:
    // myclass.cpp
    
    myClass::myClass (void)
    {
       x = 0;
    }
    Code:
    // mymain.cpp
    
    #include <iostream.h>
    
    #include "myclass.h"
    
    int main (void)
    {
       myClass m;
    }
    I cannot get a simple project like this to execute. I am using MSVC 6.0 and I add myclass.cpp to the Source Files folder but the class cannot be seen properly - I get errors such as "myclass.cpp: myClass is not a class".

    There is no "Cannot find 'myclass.h'" error so the header file can be seen but it seems like it's just being ignored.

    How do you put a simple multi-file project together?
    MSVC++ 6.0

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    in myclass.h put at the end of your code:
    Code:
    #include "myclass.cpp"

  3. #3
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I heard it was bad practice to include source files.
    MSVC++ 6.0

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Nope, I've read many books / tutorials and apcs class packets that state that its good practice to have them in seperate files so the prototypes can be seperate from the definitions for client ease of use. If they are in seperate files you MUST include them or the compiler would have no way to link them.

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I mean, I heard:

    Code:
    #include "x.cpp"
    ...would be wrong. Is it something to do with the extern keyword or make files?
    MSVC++ 6.0

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    No thats perfectly fine, but I know what your talking about...You mean that you shouldn't include cpp files in your main cpp. It is a necessity to include them in your header though or it won't link. you dont need extern here or make files. Just add the #include "myclass.cpp" in myclass.h trust me, you can look it up on google if you want. I've seen some examples on the internet

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Its because you have both .cpp's part of your Project List right? (both .cpp's should be in the Makefile). Therefor you will need #include "myclass.h" in myclass.cpp. That should work, and of course put an #ifndef/#define/#endif in that .h for safety.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well for client ease of use, its stated that its easier to include the prototypes (myclass.h) in the client app and have myclass.h include myclass.cpp so the client doesnt have to see the code behind the declerations.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    // myclass.h
    #ifndef MYCLASS_H_
    #define MYCLASS_H_
    
    class myClass
    {
    public:
       myClass (void);
    
    private:
       int x;
    };
    
    #include "myclass.cpp"
    
    #endif
    
    ----------------------------------------------------
    // myclass.cpp
    
    myClass::myClass (void)
    {
       x = 0;
    }
    
    -------------------------------------------------------
    // mymain.cpp
    
    #include <iostream.h>
    
    #include "myclass.h"
    
    int main (void)
    {
       myClass m;
    }

  10. #10
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Excellent - thanks a lot!
    MSVC++ 6.0

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    myclass.cpp needs to know the declarations (thats the .h code) and so must mainclass.cpp (by including the .h) but the mainclass.cpp wont know the definitions when compiling by just including the .h (even if its in the same project list) until it gets to the linker. This wouldnt give the definitions prehand so whats wrong with it again?

    Code:
    // myclass.h
    #ifndef _MYCLASS_H_
    #define _MYCLASS_H_
    
    class myClass
    {
    public:
       myClass (void);
    
    private:
       int x;
    };
    
    #endif
    
    ----------------------------------------------------
    // myclass.cpp
    
    #include "myclass.h"
    
    myClass::myClass (void)
    {
       x = 0;
    }
    
    -------------------------------------------------------
    // mymain.cpp
    
    #include <iostream.h>
    
    #include "myclass.h"
    
    int main (void)
    {
       myClass m;
    }
    EDIT: if it isnt seeing the code behind the declarations (myclass.cpp) when compiling, then if you are using Dev-Cpp /etc it will not know to update the .object file, and therefor will not be using an updated version of myclass.cpp. I tested. I changed x to 5, and it still said it was 0.
    Last edited by Dae; 07-08-2005 at 05:43 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    When the compiler includes the .h in your example code Dae there is no refference to myclass.cpp so it wont include it. Its better for a client to include .h in the main.cpp and not the .cpp of the class

  13. #13
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    When the compiler includes the .h in your example code Dae there is no refference to myclass.cpp so it wont include it. Its better for a client to include .h in the main.cpp and not the .cpp of the class
    Isnt that what a Project List, or Makefile is for? you include the .cpp in your Makefile, and the .h's in your .cpp's, which solves all problems.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    JoshR, you should never #include a cpp file. The only potential exception is when you work with templates, and even then there are other solutions.

    As Dae says, the proper way to do it is to add each cpp file to your project or makefile. Use Dae's example for the proper way to setup multiple source files (with one minor change - don't use leading underscores followed by an uppercase letter, just remove that leading underscore from the _MY_CLASS_H_ part).

  15. #15
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    No leading underscore? is it because the standard files contain leading underscored defines/variables? ahh that would make sense.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

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