0

Dear c++ guru:

In c++, normally we will call a function as shown below:

int funct ()
{
      ...;
}

void main()
{
      funct();
}

main will call the function "funct()" which is defined within the same file.

However, in my case, i wish to have the "funct()" on the other file while the "main()" is in a file as shown below:

filename: File1.C

void main()
{
      funct(); //funct() from File2.C
}
filename: File2.C

int funct()
{
      ...;
}

Is that possible? How can I achieve that?


Thanks,

Best regards,
Mei Ying

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Suraine
0

>>Is that possible
Yes it is, in fact its common for large programs to have hundreds of *.cpp files.

>>How can I achieve that?
The most common way is to create a header file and include it in each of the *.cpp files

// myfile.h header file
extern void func(); // function prototype
filename: File1.C
#include "myfile.h"
void main()
{
      funct(); //funct() from File2.C
}

Depending on the compiler you are using you will also have to compile each of the *.cpp files and link them together with all the required libraries. How to do that depends on your compiler.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.