 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Charlie Guest
|
Posted: Wed Sep 29, 2004 8:40 pm Post subject: Calling functions in other files... |
|
|
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?
|
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Wed Sep 29, 2004 8:46 pm Post subject: Re: Calling functions in other files... |
|
|
Charlie wrote:
| Quote: | 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.
--
Phlip
http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Sep 29, 2004 9:27 pm Post subject: Re: Calling functions in other files... |
|
|
"Charlie" <charlie.lamothe (AT) gmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Nils O. Selåsdal Guest
|
Posted: Wed Sep 29, 2004 9:31 pm Post subject: Re: Calling functions in other files... |
|
|
Charlie wrote:
| Quote: | 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)
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Wed Sep 29, 2004 9:38 pm Post subject: Re: Calling functions in other files... |
|
|
Charlie wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Thu Sep 30, 2004 1:31 pm Post subject: Re: Calling functions in other files... |
|
|
Charlie wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
charlie.lamothe@gmail.com Guest
|
Posted: Wed Oct 06, 2004 6:14 pm Post subject: Re: Calling functions in other files... |
|
|
Thanks everyone, I actually figured this out later in the day, this
confirms that i'm doing it the right way
:) Charlie
Thomas Matthews wrote:
| Quote: | Charlie wrote:
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
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|