A VERY Simple Question about COMPILING

Started by
1 comment, last by DisKordCoder 23 years, 5 months ago
Example: I have file1.cpp and file2.cpp. In file1.cpp i have make a function called ''Function1''. Now, file2.cpp uses this function in its code, but problem is that the complier compiles file2.cpp before file1.cpp, and so the compiler compains about function1 not being declared. At the moment i am creating prototypes to shut up the compiler (e.g. putting function1''s prototype into file2.cpp so that it knows it exists). Is there a way around this?? Im using Visual C++. Or is there a Technique? Thanks.
Advertisement
Ok this is what you do. Make a header file and put the function declarations in it. Then in one of your cpp files, include that header and type the function definitions. Now, if you include that header file in any cpp, your functions will work fine.

Snyper
=============================Where's the 'any' key?=============================
Yes, that is what you want to do. Let me expand on this further: For every cpp file, there should usually be an accompanying .h file with all the function definitons in it. I'm fairly sure this is common design practice (I'm about 98% sure that 90% of coders do this). You'll also want to make sure the .h can only be included once using something along the lines of:
#ifndef HEADERNAMESOMETHING#define HEADERNAMESOMETHING//blah..#endif  


You could also use '#pragma once', but this may not work with compilers other than MSVC (which is probably something you're not worried about at this point anyway )

Edited by - A. Buza on October 30, 2000 11:49:30 PM

This topic is closed to new replies.

Advertisement