Author |
Message |
Geminias
|
Posted: Tue Nov 08, 2005 7:09 pm Post subject: Hey Importing functions in C++? |
|
|
i mine as well be asking this question specifically to wtd lol. wtd, how do you import a function you've created already so you don't have to keep writing the same functions (or copy pasting them) into new source codes.
for instance in turing there's a length function built in. I'd like to make a file a cache for all the functions i find useful and thus be able to easily transport them around into new sources.
am i thinking about this the wrong way? is there something better than doing this? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Tue Nov 08, 2005 7:15 pm Post subject: (No subject) |
|
|
First off... what are you trying to find the length of?
Next, the mechanism you're looking for are header files.
There's a simple way to do this, and a correct way.
The simple way is to put everything in a header file that might look like:
c++: | #include <iostream>
void foo()
{
std::cout << "hello" << std::endl;
} |
And you might call this "foo.h".
In the same directory you'd create "main.cpp".
c++: | #include "foo.h"
int main()
{
foo();
return 0;
} |
|
|
|
|
|
|
wtd
|
Posted: Tue Nov 08, 2005 7:22 pm Post subject: (No subject) |
|
|
Now, the right way acknowledges that header files should not contain executable code. So, we separate "foo.h" into...
foo.h:
foo.cpp:
c++: | #include <iostream>
#include "foo.h"
void foo()
{
std::cout << "hello" << std::endl;
} |
Then main.cpp looks exactly the same as it did before, but when we compile it, we provide both source files.
Instead of:
code: | g++ main.cpp -o program |
We compile with:
code: | g++ main.cpp foo.cpp -o program |
|
|
|
|
|
|
Geminias
|
Posted: Tue Nov 08, 2005 7:33 pm Post subject: (No subject) |
|
|
ahhhhhhh, i read about header files and made one with for a class, but since i barely understand the concept of classes i didn't think header files were good for functions.
anyway. danke sir. |
|
|
|
|
|
Geminias
|
Posted: Tue Nov 08, 2005 7:37 pm Post subject: (No subject) |
|
|
oh and to answer your question i'm not trying to find the length of anything at the moment. i was just using length as an example function. i've already made functions to calculate factors of numbers, calculate length of words, make all lower case letters upper case, and so on... and i was looking for a way to implement them the correct way for a programming competition question. |
|
|
|
|
|
wtd
|
Posted: Tue Nov 08, 2005 7:51 pm Post subject: (No subject) |
|
|
Ah. |
|
|
|
|
|
|