I have a very simple program that doesn't seem to be working, and I can't find the root of the problem. This program is divided into 3 parts: the Header.h file, the Header.cpp file, and the Main.cpp file. In Header.h I define the functions, in Header.cpp I actually have the code for the functions, and in Main.cpp I call the functions.
Here is Header.h:

I really don't know what these mean, so if someone could explain them to me so I could correct my code, I would be grateful. Thanks.

(By the way, the code is supposed to find the area of a square, a rectangle, and a circle, subsequently. I know this way is really complicated, but as you can see, I need practice. :rolleyes: )

DaWei

20Oct2007 07:07

Re: Including multiple files

You should never put actual code in a header file. Header files are designed to make declarations available to a number of source files. Declarations tell the compiler how functions are supposed to work, and that there will be variables defined somewhere prior to the link process.

If you put actual definitions into a header file, and include it in more than one source file, multiple definition errors will be the result.

You need to understand how the build process works. First, the preprocessor runs. This will copy the header files and paste them directly into the source file. This happens before the compiler runs. The compiler works with this end result.

In your case, since you have not included a code file into multiple files, but only into main.cpp, you should not get errors. You should merely get warnings that you have done something useless.

What you want to do here is to make a project with more than one source file. Your utility file (what your are now calling header.cpp) should be part of the project, and compiled separately. It does not need to include header.h, since it defines those procedures.

Your main file, main.cpp, will also be compiled separately. It needs to include the header.h file so that it know if your are calling the utility procedures correctly.

After these two file are compiled, they are linked. This means that the linker must have access to the actual code generated by header.cpp, not just explanations of how to call the functions. The linker will reconcile the two object files generated by the compilation and put the actual address of the functions in header.cpp into the calls seen in main.cpp.

These ultimate addresses may be modified by the OS (or they may not), but even if they are they will be in agreement and will result in the desired operation.

Lief Webster

20Oct2007 19:00

Re: Including multiple files

I'm not sure I quite understand everything, so I tried removing the line:
#include "Header.cpp"
and compiling it, but now I get a linker error.

Sorry if I seem to be asking something obvious, but now what do I do to make this run correctly?

Lief Webster

20Oct2007 19:06

Re: Including multiple files

Scratch that: apparently they weren't all in the same project when I did that, so I added all of them to a project and tried to compile. This time my errors list was much bigger:

Projects/Main.cpp:3:20: iostream: No such file or directory
Projects/Main.cpp: In function `int main()':
Projects/Main.cpp:18: `cout' undeclared (first use this function)
Projects/Main.cpp:18: (Each undeclared identifier is reported only once for
each function it appears in.)
Projects/Main.cpp:27: `cin' undeclared (first use this function)

make.exe: *** [Projects/Main.o] Error 1

Execution terminated

Apparently, it's not reading my #include <iostream> and using namespace std; lines. How can I fix this?

DaWei

20Oct2007 19:16

Re: Including multiple files

Quote:

undefined reference to `WinMain@16'

This means that you have set up your project to be a Windows project, not a console project. It has nothing to do with your code, per se. A Windows project expects to find a WinMain rather than a main.

You can mess around with your compiler settings and eventually succeed in changing this, but your quickest way out is to start a new project, making sure it's a Console project. That would be Win32, Win32 Console Application, under the File/New/Project menu. Then, when you get to Application Settings, select Console Application, deselect "Precompiled Headers" and select "Empty Project".

After that's done, go to Project/Add/Add New Item/, Select Code and .cpp file. Copy your "main.cpp" file into that new file. Do the same for "header.cpp". Then do the same for "header.h", except choose Code and header (.h) file.

Compile and run your app.

Incidentally, choosing to use the names "main" and "header" is a rather sucky thing to do. Main is a generic function used for launching a C/C++ application, and header files are thick on the ground. Use something descriptive of the file's contents.

Lief Webster

30Oct2007 07:05

Re: Including multiple files

Alright I finally fixed and compiled it, but now when I try to run it, it displays three 0s instead of the numbers it's supposed to. It should be taking x * x (or 5*5) and display 25, but it comes up as 0 for all three functions.

You are outputting a variable which you are not assigning. You are assigning it to the local variable inside the function and not to the Global One which you are printing in the main.

Lief Webster

31Oct2007 02:37

Re: Including multiple files

Alright, since I tried to fix it and failed miserably, could you explain the scope of variables when including multiple files? Am I allowed to access, for example, the int z from Main.cpp to use in a function in Header.cpp? Am I able to pass variable through multiple files using functions?

shabbir

31Oct2007 08:07

Re: Including multiple files

Its does not depend on multiple files and its just a matter of knowing what is the scope of a variable.