473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Jul 22 '05 #1
6 2737
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?


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/bi...UserInterfaces

Jul 22 '05 #2

"Charlie" <ch*************@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
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
Jul 22 '05 #3
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?

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)
Jul 22 '05 #4
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?


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


Brian Rodenborn
Jul 22 '05 #5
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

Jul 22 '05 #6
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:
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


Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Julia Briggs | last post by:
Hello World - I admit I'm new to javascript but I've tried for days to find a solution to my problem. Basically I have 3 unique javascript files that do different screen display events that I...
0
by: Denis | last post by:
Hello, I am working on a large business application written mostly with VC++ and MFC. What I'm trying to do is accessing my database with the existing functions in my c++ application using XML...
5
by: Andrew Slade | last post by:
Hello, I am making calls from one compilation unit to functions in another by pointers and I get the warnings below from gcc on 2.4.x Debian Linux. The executable seems to work fine but the...
19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
6
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally,...
2
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.