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