Sunday, August 2, 2009

Multiple c++ files?

i have 3 cpp, file1.cpp, file2.cpp, file3.cpp


file1.cpp: base class


file2.cpp: derived class of file1


file3.cpp: main()





in file2.cpp, i did # include "file1.cpp", but i gt and error, LNK2005





How do i include additional cpp files?

Multiple c++ files?
Thats not right, you have to use the header files so if you wanted to include file1.cpp into your file2.cpp and file2.cpp into file3.cpp


you have to do this:





in file2.cpp


#include "file1.h"





in file 3


#include "file2.h"





This mean you have to create file1.h and file2.h. if you just change the names of those, it still wont work.





Look up on google how to create header files in C++
Reply:You can't assemble source files with #include (that's for headers), you need to add all cpp (c,c++ source) files to the project (in Dev for example, you'd right click on project inside the project window, add file -- when creating a new project, you also need to remove the auto-generated main file).





Hope that helps...
Reply:do not include file1.cpp rather move class definition in file1.h and include file1.h
Reply:The most proper way to #include also have #ifdef #define peppered within them.





and that custom .cpp .h files use "" not angle brackets for inclusion.
Reply:What are you using to compile and build? Are you using an IDE like VC++ or g++?





If you are using an IDE, just add all the .cpp and .h files to the project. You don't #include other .cpp files, you include their headers .h files. The header file contains all the declarations a .cpp might need to build.





If you use g++ just put all the files on the command line as options





%26gt; g++ -o myprog file1.cpp file2.cpp file3.cpp


No comments:

Post a Comment