-2

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?

marumaru
  • 55
  • 5
  • 6
    Don't #include a *.cpp file. – Eljay Dec 19 '17 at 03:48
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user0042 Dec 19 '17 at 03:50
  • _@marumaru_ Hold on! Don't deny my duplicate proposal because there's not the same question you're asking about. All the answers are there to solve your particular problems. `#include` header files and link translation units. – user0042 Dec 19 '17 at 03:52
  • 1
    Possible duplicate of [Why should I not include cpp files and instead use a header?](https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header) – Retired Ninja Dec 19 '17 at 04:02

2 Answers2

3

You don't include CPP files into other CPP files. Instead, you make a header for it, and include it instead. In your case the header would be very short:

blah.h:

int blah();

Now replace "blah.cpp" with "blah.h", and compile the code the way you did to get it to work.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

If you include blah.cpp in a.h, then you have included it in b.h as well. Because b.h inherits a.h, so it will inherit all a.h included files.

             a.h <--- //here you include blah.h(never .cpp)
            /   \
           /     \
        a.cpp    b.h
                   \
                   b.cpp

So the methods from blah.h you can use it now in a file and b file. And in main you include a.h and it's suffice.

lukuss
  • 128
  • 7