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

>>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.

Thanks, Ancient Dragon.
I got what u mean.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.