PDA

Click to See Complete Forum and Search --> : calling a function from another source file included in the project


kourosh
Feb 3rd, 2001, 04:40 AM
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

sail3005
Feb 3rd, 2001, 10:56 AM
why don't you just make a header file, like colorfunctions.h?

parksie
Feb 3rd, 2001, 11:47 AM
sail has the right idea here, but you need a source file to go with it:

colours.h

#ifndef __COLOURS_H__
#define __COLOURS_H__

void functionA();
int functionB(int);

long OtherFunction(char*);

#endif // __COLOURS_H__


colours.cpp

#include "colours.h"

// put the functions here


main.cpp

#include <iostream.h>
#include "colours.h" /* important - quotes not angle-brackets! */

void main() {
OtherFunction(...);
}

Feb 3rd, 2001, 12:53 PM
How does the .h file know where to look for the functions if you haven't included the .cpp file with it?

parksie
Feb 3rd, 2001, 04:55 PM
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 :rolleyes:

Feb 3rd, 2001, 05:22 PM
Cool :)

kourosh
Feb 4th, 2001, 12:07 AM
Thanks alot for the replies, I am going to try this approach but I probably would fail. Anyhow thanks alot again.

kourosh
Feb 4th, 2001, 03:27 PM
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> ?

parksie
Feb 4th, 2001, 03:30 PM
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.

kourosh
Feb 4th, 2001, 03:36 PM
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??

sail3005
Feb 4th, 2001, 03:40 PM
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.

Feb 4th, 2001, 04:03 PM
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....

parksie
Feb 4th, 2001, 04:28 PM
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 :)

kourosh
Feb 4th, 2001, 04:57 PM
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++. :)

parksie
Feb 4th, 2001, 04:59 PM
Hehehe:

"Compiler Error C6660: Your variable name is incorrectly spelt"