Calling functions in other files...

Discussion in 'C++' started by Charlie, Sep 29, 2004.

  1. Charlie

    Charlie Guest

    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?
     
    Charlie, Sep 29, 2004
    #1
    1. Advertising

  2. Charlie

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Sep 29, 2004
    #2
    1. Advertising

  3. "Charlie" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 29, 2004
    #3
  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?

    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)
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 29, 2004
    #4
  5. Charlie

    Default User Guest

    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
     
    Default User, Sep 29, 2004
    #5
  6. 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
     
    Thomas Matthews, Sep 30, 2004
    #6
  7. Charlie

    Guest

    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
     
    , Oct 6, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Bos

    calling other functions

    Richard Bos, Nov 14, 2003, in forum: C Programming
    Replies:
    11
    Views:
    537
    Florian Weingarten
    Nov 15, 2003
  2. Andrew Slade
    Replies:
    5
    Views:
    432
    Andrew Slade
    Dec 31, 2003
  3. Dominic
    Replies:
    1
    Views:
    357
    Scott David Daniels
    May 17, 2004
  4. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  5. Tony Doyle

    Calling other functions

    Tony Doyle, Nov 27, 2006, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    124
    Tony Doyle
    Nov 28, 2006
Loading...

Share This Page