This is part of the problem with integrated development environments, specifically with using them to learn. When you're au fait with everything they do that's the time to start using them.

No, there are several file types. cpp and h are the source files, but that's just the start.

The first step is to preprocess the file. This sorts out the lines beginning with #, so the #includes, #if, #else etc. Also the preprocessor typically strips out comments.

The output of the preprocessor is fed into the compiler, which produces object code. This is almost executable, but has a variety of links into other object files andlibraries that need resolving. That resolving is the job of the linker which takes multiple object files and produces an executable.

With an IDE you just hit Build and it gives you an executable. That's why you shouldn't learn with an IDE but at the command line.

In your code, main calls function add() which is in a different source file. If you just tried to make an executable with main.cpp, you'd get an unresolved symbol error. Try it - familiarise yourself with that error because you'll see a lot of it and it's useful to know how to fix it.

Have a look at the files in your Debug folder where you created the project. Chances are you'll find main.obj. That's the object code.

If you now add post.cpp to the project...

[aside: if you have JUST post.cpp in the project and try to build the executable, then the unresolved symbol will be main itself]

... then this will define add(), which will be in post.obj, and the linker will now correctly build main.exe.

At the moment, post.cpp doesn't need to include iostream.h or use namespace std. You can remove both those lines and it will work fine.


Now, there is another error in the code. main.cpp has no definition for add(), hence the error you get. So there are a couple of ways of doing this. You can prototype add() directly in main.cpp by adding "int add(const int x,const int y);", which is a function prototype, above main(), or you can create the prototype in a header file and #include it in main.cpp. You don't need to include that header file in post.cpp (not yet, anyway, you might if post.cpp will be extended with other functions that call each other and aren't in the right order for the compiler to know what they are before they're used).