Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Including multiple files (http://www.go4expert.com/forums/including-multiple-files-t6934/)

Lief Webster 20Oct2007 06:44

Including multiple files
 
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:

Code:

//Header.h

double SquareArea( double );

double Rectangle ( double );

double Circle ( double );

Here is Header.cpp:

Code:

//Header.cpp

double SquareArea ( double x )
{
    double squarearea;
    squarearea = x * x;
    return squarearea;
}

double Rectangle ( double y )
{
    double recarea;
    recarea = y * y;
    return recarea;
}

double Circle ( double z )
{
    double circarea;
    circarea = z * z * 3.14;
    return circarea;
}

Here is Main.cpp:

Code:

// Main.cpp

#include <iostream>
#include "Header.h"
#include "Header.cpp"

using namespace std;

//Global variables
double squarearea , recarea , circarea;

int main()
{
    double x = 5;
    double y = 5;
    double z = 5;
   
    SquareArea ( x );
    cout << squarearea;
   
    Rectangle ( y );
    cout << recarea;
   
    Circle ( z );
    cout << circarea;
   
    int a;
    cin >> a;
    return 0;
}

When I try to run these three files together, I get a few errors:

Main.o(.text+0x0):Main.cpp: multiple definition of `SquareArea(double)'
Header.o(.text+0x0):Header.cpp: first defined here
Main.o(.text+0x2c):Main.cpp: multiple definition of `Rectangle(double)'
Header.o(.text+0x2c):Header.cpp: first defined here
Main.o(.text+0x60):Main.cpp: multiple definition of `Circle(double)'
Header.o(.text+0x60):Header.cpp: first defined here

make.exe: *** ["Square] Error 1

Execution terminated


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.

C:\Dev-Cpp\lib/libmingw32.a(main.o)(.text+0x7f):main.c: undefined reference to `WinMain@16'

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.

(Yes, I know it's quite a sucky thing to do, naming my files like that. Sorry if it upset you. :) )

How can I get it to show the numbers it's supposed to be showing?

shabbir 30Oct2007 08:37

Re: Including multiple files
 
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.

Try running this program
Code:

#include <iostream>
using namespace std;

int iTemp = 10;

FuncLocal()
{
    int iTemp; // Declare the variable once again
    iTemp = 100;
}
FuncGlobal()
{
    iTemp = 1000;
}

int main()
{
    cout << iTemp<<endl;
    FuncLocal();
    cout << iTemp<<endl;
    FuncGlobal();
    cout << iTemp<<endl;
    return 0;
}

What should be the output of the program?

Lief Webster 2Nov2007 02:14

Re: Including multiple files
 
Well, I suppose it would output something like this:

Code:

10
100
1000

When I tried to run it, I got errors because the functions have no type, so I asigned them both to ints.

Ah.

Code:

10
10
1000

I thought there might be some trick to this. I suppose the reason is that int iTemp is only defined as 100 in that function and when returning to main it resorts back to the global definition. Right?


All times are GMT +5.5. The time now is 06:15.