Calling another function from a file with it's own main.

This is for homework and my own understanding, so I'd appreciate being walked through the situation as oppose to being directly shown the answer so I'm going to avoid using direct code if possible.

I have (following instruction) written a very simple a.cpp file with just mainA function and function x. I was told to call function a from my main function. Then I was given another b.cpp file with it's own function y, function z, and mainB function. I was told to "use function y before and after using function x", but I am not sure how to go about calling the functions from the other file. Further, function y will not function without function z.

I intend to talk to my teacher tomorrow because I feel like I may be understanding the issue incorrectly, but I would like to try and solve this issue on my own tonight if possible, but I feel if I am not misunderstanding the isuee, there is a fundamental gap in my knowledge that I might be able to learn here.

Thanks in advance for any help, and sorry if this is just a misunderstanding of what is suppose to be done on my part.

1
2
3
4
5
6
7
8
//a.cpp
function x() {}

mainA() {
function y();
function x();
function y();
}

1
2
3
4
5
6
7
//b.cpp
function y() {};
function z() {};
mainA() {
function z();
function y();
};
It seems like you don't know how to define a function. You should take a look at this part of the tutorial and see if it helps:
http://www.cplusplus.com/doc/tutorial/functions/

Do you have the text of your assignment available? It's not clear to me exactly what you're meant to be doing.
Hmm, perhaps I didn't explain well, these are all just filler functions for the sake of simplicity with no arguments or return values. My actual functions are (mostly) void, and do take in some arguments. I assure you that stand alone, my file works fine.

Essentially my understanding of what I am suppose to do is:
Create a function.
Call it from the main method.
From the main method, call the main method of another cpp file.
Last edited on
This is not possible actually. You would need to declare the functions in a file that does not have a main. Because to use functions from another cpp file, one has to "link" two cpp files together. And the linker would see two main functions and print out some error messages. What you should do is make a third cpp file and then compile A and B separately from each other, but link both of them with the third cpp file. At that point you will have 2 separate programs that both use code from your third file.
Here is a more detailed explanation of compiling and linking:
http://www.cprogramming.com/compilingandlinking.html
Last edited on
Alright, thank you, that is what i thought was the case, it's good to hear that my understanding of the language is not what is at fault here, but rather it seems my understanding of the assignment. I guess I will be unable to finish tonight and I will have to talk to my teacher tomorrow, as I believe doing this round-about linking is not the desired method.
Topic archived. No new replies allowed.