Click to See Complete Forum and Search --> : More than one source file
SteveCRM
May 12th, 2001, 06:28 PM
How would I link two source files together in a project? Because I would prefer to keep my class in a seperate file. :)
HarryW
May 12th, 2001, 06:37 PM
Just add both source files to the project and the compiler should sort out the grouping them together thing (It will if you're using VC++ anyway). If you want to use a variable defined in one source file in another source file you'll need to 'extern' it, if ya know what I mean.
SteveCRM
May 12th, 2001, 07:02 PM
Like how would I use a class that's on the second source code?
Zaei
May 12th, 2001, 07:08 PM
If you have your main source file, we'll call it "main.cpp" and say you have your class file, called "class.h". at the top of your "main.cpp", do "#include "class.h"". Basically what this does is, at compile time, takes the entire contents of "class.h", and sticks it where you put that "#include" statement. If you called the file with your class in it "class.cpp", you CANNOT have that file also in your project. If you do, itll give you errors. Just remove it from the project, and "#include "class.cpp"".
Z.
HarryW
May 12th, 2001, 07:12 PM
Well it's usually a good idea to put class definitions in header files and include the header file in whatever needs to use it. Just put the usual #ifndef, #define, #endif compiler directives in to make sure that it doesn't include a header more than once. I hope you know what I'm talking about... if not just say so.
Generally you should put definitions in header files, and code and declarations on source files. So you only put the class definition in the header file, with just declarations of the member variables and functions. Then in a source file to go with the header file, you put the definitions of each of the member functions of the class.
HarryW
May 12th, 2001, 07:20 PM
Ugh, I hate #including .c and .cpp files, it's just ugly... seems like some kind of sacrilege... I think you should only #include header files.
Zaei
May 12th, 2001, 11:10 PM
I know what you're talking about Harry. Its disgusting.
Z.
Ekim
May 13th, 2001, 07:09 AM
that is why you have you class def in the header file and the class coe in a cpp file alot easyer, and in the class.cpp file you include the class.h file that easy and then you call the class.h file and it will do the rest.
parksie
May 13th, 2001, 08:43 AM
Yep :)
Although I usually put inlined functions in the the .h file as well (there's a split over whether to do this or put them in the .cpp file, but I do it this way to prevent me forgetting to put the extra inline into the source file and buggering the compiler over).
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.