Assuming I have a cpp simple file as such
//blah.cpp
int blah()
{
return 1;
}
and there are two sets of files where one inherits the other.
a.h:
//a.h
#ifndef A_H
#define A_H
class Blah
{
public:
Blah(int var);
int callingblah();
private:
int myvar;
};
#endif
a.cpp including a.h
//a.cpp
#include "blah.cpp"
#include "a.h"
Blah::Blah(int var) : myvar(var) {}
int Blah::callingblah()
{
blah();
return 2;
}
b.h that inherits a.h:
#ifndef B_H
#define B_H
#include "a.h"
class ChildBlah : public Blah
{
public:
ChildBlah(int var, int childvar);
int callingblah();
private:
int childvar;
};
#endif
b.cpp including b.h and also has the main method
//b.cpp
#include "blah.cpp"
#include "b.h"
ChildBlah::ChildBlah(int var, int childvar) : Blah(var), mychildvar(childvar) {}
int ChildBlah::callingblah()
{
blah();
return 3;
};
int main()
{
Blah myBlah(1);
ChildBlah myChild(1,2);
cout << myBlah.callingblah();
cout << myChild.callingblah();
}
And I compile with:
g++ a.cpp b.cpp blah.cpp
The problem here is that I get "multiple definition" error because I have included blah.cpp to multiple places.
But I can't not include blah.cpp because then I will get "not declared" error.
I have purposely not made a blah.h for the sake of education and experience. There has to be some scenarios where you just want to include the cpp and not have a header.
How do you go about to resolve this without making a header file for blah.cpp?