Calling functions in other files...

C

Charlie

Ok, I have three files: calc.cpp, calc.h, and ui.cpp.

I would like to call a function that is located in ui.cpp from main()
in calc.cpp.

Both files have calc.h included, but when I tried to include ui.cpp in
calc.cpp, it gave me an error due to multiple instances of the same
function's.

How do I call my function that is in ui.cpp from calc.cpp?
 
P

Phlip

Charlie said:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp.

I would like to call a function that is located in ui.cpp from main()
in calc.cpp.

Both files have calc.h included, but when I tried to include ui.cpp in
calc.cpp, it gave me an error due to multiple instances of the same
function's.

How do I call my function that is in ui.cpp from calc.cpp?

Your compiler must have come with sample programs showing how to link more
than one object file, and you can find a tutorial for it on Google.
 
J

John Harrison

Charlie said:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp.

I would like to call a function that is located in ui.cpp from main()
in calc.cpp.

Both files have calc.h included, but when I tried to include ui.cpp in
calc.cpp, it gave me an error due to multiple instances of the same
function's.

How do I call my function that is in ui.cpp from calc.cpp?

You never include one source file in another.

You should write a header file called ui.h that declares the function in
ui.cpp that you want to call. Then include that header file in both ui.cpp
and calc.cpp.

This is basic stuff, any decent C++ book should explain this.

john
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Charlie said:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp.

I would like to call a function that is located in ui.cpp from main()
in calc.cpp.

Both files have calc.h included, but when I tried to include ui.cpp in
calc.cpp, it gave me an error due to multiple instances of the same
function's.

How do I call my function that is in ui.cpp from calc.cpp?
Put its prototype in calc.h, don't include ui.cpp in calc.cpp
(or if you do include it, don't compile and link ui.cpp as well)
 
D

Default User

Charlie said:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp.

I would like to call a function that is located in ui.cpp from main()
in calc.cpp.

Both files have calc.h included, but when I tried to include ui.cpp in
calc.cpp, it gave me an error due to multiple instances of the same
function's.

How do I call my function that is in ui.cpp from calc.cpp?

Sounds like you have functions defined, not just declared, in your .h
file. Don't do that.




Brian Rodenborn
 
T

Thomas Matthews

Charlie said:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp.

I would like to call a function that is located in ui.cpp from main()
in calc.cpp.

Both files have calc.h included, but when I tried to include ui.cpp in
calc.cpp, it gave me an error due to multiple instances of the same
function's.

How do I call my function that is in ui.cpp from calc.cpp?

When you reference (call or take the address of) a function,
the compiler needs information about it, such as its return
type, the number of parameters, types of parameters, etc.

This can be accomplished by using a function prototype or
declaration before using the function. The common guideline
is to list all the declarations before the function definitions.

If a function in a translation unit (i.e. cpp file) is used
by other translation units, the common guideline is to place
the function header into a separate file (i.e. ".h" file) and
include it in each file that uses the function.

In your situation, you may want to create a header file, ui.h,
which contains declarations of functions that are in ui.cpp.
Include this file from calc.cpp.

Another common guideline is to only publish (create function
declarations in files) functions that are used by other
files. This will help support "encapsulation". Also declare
the functions as "static". This is commonly referred to as
"need to know" basis.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

charlie.lamothe

Thanks everyone, I actually figured this out later in the day, this
confirms that i'm doing it the right way

:) Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top