I'm trying to use multi files in a lesson, using CB 20.3 in W10.
They say, separate the add and main functions from the file below to do this. Then compile them.
This doesn't compile as the author shows. He says use a new file for the add function. Is it somehow possible ? How do I go about doing it?
The original is shown with forward declaration next.
I've separated the two file below the************ lines if that will help to copy/paste.
#include <iostream>
int add(int x, int y); // forward declaration of add() (using a function prototype)
int main()
{
std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n'; // this works because we forward declared add() above
return 0;
}
int add(int x, int y) // even though the body of add() isn't defined until here
{
return x + y;
}
************************************
add.cpp
int add(int x, int y) // even though the body of add() isn't defined until here
{
return x + y;
************************************
main.cpp
#include <iostream>
int main()
{
std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n'; // this works because we forward declared add() above
return 0;
}