Reputation: 2679
How to include file 2 in file 1. What changes I need to make in file 2.
file 1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}
file 2
int otheFun()
{
cout<<"Demo Program";
return 0;
}
Upvotes: 3
Views: 15127
Reputation: 206646
You do not include cpp files in to another cpp files.
Also, a c++ program can have only one main()
function.
If you are trying to play around with a program which has multiple files, You will need to have something like this:
file2.cpp
#include <iostream>
#include "file2.h"
int printHelloWorld()
{
std::cout<<"Hello World";
return 0;
}
file2.h
#ifndef FILE2_H <----Lookup Inclusion Guards on google, this is important concept to learn.
#define FILE2_H
int printHelloWorld();
#endif //FILE2_H
file1.cpp
#include <iostream>
#include "file2.h"
int main()
{
std::cout<<"Demo Program";
printHelloWorld();
return 0;
}
Upvotes: 11
Reputation: 385405
What changes I need to make in file 2?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
cout << "Demo Program";
}
Upvotes: 1