Thread: How to Access a Function from Multiple Files?

  1. #1
    Unregistered
    Guest

    Question How to Access a Function from Multiple Files?

    I'm trying to write a function in a file, and be able to use it in multiple other files in that project. Is there a way to do this?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Yes, you create a .cpp file with the function you write, then in other files you #include "thefile.cpp" into the other cpp files you want to use the function in

    ex

    mystring.cpp has string functions you wrote.
    then in main.cpp
    #include "mystring.cpp" would give you access to those functions

  3. #3
    Unregistered
    Guest
    Hmm thanks, but I have the following and it doesn't work:

    test.cpp:
    ----------------------------
    #include " test2.cpp"

    int main(void)
    {
    hi();

    return 0;
    }
    ----------------------------

    test2.cpp:
    ----------------------------
    #include<iostream.h>

    void hi(void)
    {
    cout << "hi" << endl;
    }
    ----------------------------


    I get this error:

    Compiling...
    test2.cpp
    Linking...
    test2.obj : error LNK2005: "void __cdecl hi(void)" (?hi@@YAXXZ) already defined in test.obj
    Debug/test.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.


    Thanks!

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Ok, welcome to the world of header files. Create a header file named test2.h and in it put the function protoypes that you would like to use:

    Code:
    //-----------test2.h---------------------------
    
    void hi();
    Then create test2.cpp and place the function definitions prototyped in the header file.
    Code:
    /*---------------test2.cpp------------------------*/
    
    #include "test2.h"
    #pragma once
    #include <iostream>
    
    using namespace std;
    
    void hi(){
              cout<<"hi"<<endl;
    }
    Then in your main file include your header file and let the linker do the rest:
    Code:
    /*---------------test.cpp---------------------------*/
    #include "test2.h"
    #pragma once
    #include <iostream>
    
    using namespace std;
    
    int main(){
              hi();
              cin.get();
              return(0);
    }

  5. #5
    Unregistered
    Guest
    Great, thanks!

  6. #6
    Unregistered
    Guest
    One more question: I notice you still include iostream in main even though you're also including test2 which has iostream included. For example:

    hi.cpp:
    Code:
    #include "hi.h"
    #include<iostream.h>
    void hi()
    {
    cout << "hi" << endl;
    }
    main.cpp
    Code:
    #include "hi.h"
    #include<iostream.h> //do I still need this?
    
    int main()
    {
    hi();
    cin.get();
    return 0;
    }
    Thanks!

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    You don't nned it as long as your main.cpp file doesn't access anything found in that file, aka cin, cout. However if they do then yes you do still need to declare it. However this isn't as gross of memory use as one may seem to believe due to the '#pragma once' preprocessor directive which will have the compiler add the include file only once.

  8. #8
    Unregistered
    Guest
    Ok, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to use argv to access multiple files
    By DonFord81 in forum C Programming
    Replies: 12
    Last Post: 04-03-2009, 09:32 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Multiple Files and Struct
    By gsoft in forum C Programming
    Replies: 4
    Last Post: 01-12-2005, 04:44 AM