-3

Say I have 4 classes A, B, C, and D, and I want to use all 4 within a class that is implementing pure virtual functions from another .h file User. My question is how can I include those 4 classes into one .h so the .cpp of that .h will allow me to call those functions within the classes.

This is what I have tried so far:

#include "User.h"
#include "A.h"
#include "B.h"
#include "C.h"
#include "D.h"

Class create : public User, public A, public B, public C, public D
{
public:
    create();
    virtual ~create();

    bool a1();
    bool b1();
    bool c1();
    bool d1();

    int a2();
    int b2();
    int c2();
    int d2();
};

Then when I try to call those functions (declared in A.cpp, B.ccp, C.cpp, D.cpp) in the create.cpp I am being told "undefined reference to A::a1( )... and so on.

Does this make sense?

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
Freddy Roller
  • 117
  • 11
  • Are you linking to the compiled objects from A.cpp etc? – GWW May 26 '15 at 22:58
  • In C++ terms, there is no need to have a correlation between file content and its name. You are safe to do whatever you like. It is perfectly safe to define a Plant class in Animal.h. As long as you properly link files, it will work. – Seçkin Savaşçı May 26 '15 at 22:59
  • Please make a short example that we can copy and paste to our development environment and see this "undefined reference". – Dialecticus May 26 '15 at 23:00
  • I apologize I am more or less a beginner at c++. Basically eclipse is giving me the error "undefined reference to A:a1( )" in my create.cpp file. So it is linking the fact that the a1( ) function comes from the A class and so my question is how do I "define" the reference to that function. – Freddy Roller May 26 '15 at 23:18
  • @GWW how do I link those compiled objects? – Freddy Roller May 26 '15 at 23:20
  • Which os/compiler are you using? Also please post the full compiler/linker error. – Paul Rooney May 26 '15 at 23:37

1 Answers1

1

The undefined reference to ... error is not a compilation error. It is a linker error.

Basically, the linker is a program that puts together multiple pieces of object code (i.e. compiled code) to create an executable file. In your case, the compiler knows that a method A::a1() must exist, agrees with its declaration (the name and parameters) and doesn't complain about its usage. However, the liker cannot find its definition (basically, its compiled implementation).

You provided too few details for an explanation of how to properly compile and link your code, but the process should consist of compilation of each .cpp file, that contains implementations of your classes. Some details about linking can be found in the following questions:

C++ linking object's files (G++)

Linking files in g++

You probably need to do something like this:

g++ source_code.cpp A.cpp B.cpp C.cpp D.cpp

or something like that. If you need to link the object code, replace the .cpp extension with .o once you compile each individual .cpp file into an .o file.

Community
  • 1
  • 1
Paul92
  • 8,827
  • 1
  • 23
  • 37
  • so i need to replace the .cpp files with .o files? – Freddy Roller May 26 '15 at 23:21
  • @FreddyRoller: as I said in my answer, there is too less information in your question to be able to tell you exactly how to do it. But the main idea is that GCC needs to know about all classes (their implementation), either by giving it their source code or their .o files. – Paul92 May 27 '15 at 15:03