I am try to place a class in a separate file using c++ but without including the .cpp file it is not working.
This is CPP File of the Class Example
//Example.cpp
#include "Example.h"
#include<iostream>
using namespace std;
Example::Example()
{
cout<<"I am am Executed\n";
}
This is header file
//Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
class Example
{
public:
Example();
};
#endif
Now If I don't include the C++ file into my main function file it gives out an error
#include "Example.h"
//#include "Example.cpp"
#include<iostream>
using namespace std;
int main()
{
Example aak;
return 0;
}
So here I have commented out the //#include "Example.cpp", it would give me an error stating that
/tmp/ccuHMRJB.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `Example::Example()'
collect2: ld returned 1 exit status
However If I uncomment the #include "Example.cpp" it works just fine! Giving me an output
I am Executed!
I don't understand why I need to include both of the files (.h as well as .cpp) to execute the program. As long as I think it should work by just including the .h file but it doesn't....