Results 1 to 15 of 15

Thread: calling a function from another source file included in the project

  1. #1

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Question

    I was writting an small program and then I had to change the color rapidly, I thought maybe it is possible to write a big source file with lots of functions and each function be one color and then call the functions from another source file in the project whenever I need it. In this way I can include this source file in all of my projects and I can use it whenever I need to change the coloring.

    If there is a way I appreciate it if you tell me.

    Thanks in advance
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  2. #2
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    why don't you just make a header file, like colorfunctions.h?

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    sail has the right idea here, but you need a source file to go with it:

    colours.h
    Code:
    #ifndef __COLOURS_H__
    #define __COLOURS_H__
    
    void functionA();
    int functionB(int);
    
    long OtherFunction(char*);
    
    #endif // __COLOURS_H__
    colours.cpp
    Code:
    #include "colours.h"
    
    // put the functions here
    main.cpp
    Code:
    #include <iostream.h>
    #include "colours.h" /* important - quotes not angle-brackets! */
    
    void main() {
        OtherFunction(...);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Guest
    How does the .h file know where to look for the functions if you haven't included the .cpp file with it?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Because it's not the compiler's job.

    The .h file is for the compiler, and tells it to expect these functions. The compiler compiles that .cpp file to an .obj file. The linker then matches the dependent functions in each .obj file when it creates the final .exe

    Simple really
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Guest
    Cool

  7. #7

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Talking

    Thanks alot for the replies, I am going to try this approach but I probably would fail. Anyhow thanks alot again.
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  8. #8

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Question how about this?

    I red that if you save the h file in your current work space then you should use "" and I was wondering, if you add it into your include folder can you use by saying

    #include <colorfunction.h> ?
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep. However, you should never add anything to your main includes folder. The best way is to set aside another folder, and add that to the list of includes folders. That way you can use <colorfunction.h> (RANT: can't anyone spell???) in your programs.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256
    Originally posted by parksie
    Yep. However, you should never add anything to your main includes folder. The best way is to set aside another folder, and add that to the list of includes folders. That way you can use <colorfunction.h> (RANT: can't anyone spell???) in your programs.
    Acording to what you said I should another folder to my include folder and add the user defined libraries in that. Right? So when I am using the #include <colorfunciton.h> should I also specify the path to the folder?

    Let's say the folders name is udefined, shouldn't I use
    #include<udefined\colorfunction.h>
    or would the compiler automatically search all the include folder incloding the subfolders??
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  11. #11
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    why is it bad to add files to the compilers include folder? I did, and everything seems, maybe i screwed something up i didn't even knowabout.

  12. #12
    Guest
    It's bad to add files to the compilers include directory, because you may replace something that is needed, and the directory should be used just for the compilers stuff...

    what I do is make a directory, c:\include

    and put all my extra files in there.. then on whatever compiler I have, I add an extra include path(paths are seperated by semicolons). then all you have to do is include it normally..
    #include <colorfunctions.h> (The compiler looks in both directories for the file)



    Mike:

    It may seem wierd to you, but color is normal Americanese... if we were to spell colour on a report at school or something, it would be marked incorrect....

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For the spelling...I feel really sorry for you guys

    Nah, it's okay -- I'm not totally bothered...I'm just a perfectionist so ignore me
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Thumbs up Thanks

    Thanks alot to everybody I think I got what I was looking for.

    Parksie, don't worry about it. I didn't notice what you wrote about spelling and anyway I admit my spelling is sux. . Although it's really bad that my spelling is not good. Man I am wondering what was Bill thinking about! He should have integrated an spell check with the C++.
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hehehe:

    "Compiler Error C6660: Your variable name is incorrectly spelt"
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width