Properly Define Constructor/Destructor

Can someone teach me the proper method to define a constructor/destructor?

I am trying to bypass the following error:

1
2
 myPhysListHadron.cc:(.text+0x4c5): 
 undefined reference to `vtable for G4LEKaonZeroInelastic' 


which are caused by improperly defined constructor/destructor for a virtual process.

Here is the constructor/destructor in class G4LeKaonZeroInelastic which I assume is causing the problem (although it looks correct to me):

1
2
3
4
5
6
7
    G4LEKaonZeroInelastic() : G4InelasticInteraction("G4LEKaonZeroInelastic")
    {
      SetMinEnergy( 0.0 );
      SetMaxEnergy( 25.*GeV );
    }
    
    ~G4LEKaonZeroInelastic(){} 
I have a huge list of these 'undefined reference to vtable' linker errors which I assume are all being caused by improper constructor/destructor definition, all referring to different classes called by the same cpp file.
Make sure you have defined all pure virtual functions.
I looked at the linker errors more closely, I think the problem is in the virtual constructor on line 18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef myPhysListHadron_h
#define myPhysListHadron_h 1

#include "G4VPhysicsConstructor.hh"
#include "globals.hh"


class myPhysListHadron : public G4VPhysicsConstructor 
{
  public:
    myPhysListHadron(const G4String& name = "hadron");
    virtual ~myPhysListHadron();

  public:
  // Construct particle and physics
  virtual void ConstructParticle();
  //
  virtual void ConstructProcess(); 

};

#endif 


Last edited on
Should I close with a deconstructor via:

~ConstructParticle(){}

*Separate question, are curly braces required for a single line constructor/destructor or can I get away without them? i.e. which method is correct?

virtual void ConstructParticle();

or

virtual void ConstructParticle(){};
You need to define the destructor so you need the curly braces somewhere. If you leave them out in the class definition you would have to define the destructor elsewhere.
> I assume are all being caused by improper constructor/destructor definition
They are caused by leaving out the definition of a non-pure virtual member function.

> Should I close with a deconstructor via:
> ~ConstructParticle(){}
¿do you have a `ConstructParticle' class?
in your code `ConstructParticle()' is a member function of `myPhysListHadron'
I think the problem is in the virtual constructor on line 18

1) Does the G4VPhysicsConstructor have any pure virtual functions?
If so, you must overload those functions and provide an implementation for them. Not doing so can cause vtable errors.

2) Do you have other classes deriving from myPhysListHadron? The implication would be that you do, since you've declared ConstructParticle() as virtual. Do you have an implementation (even if a dummy) for ConstructParticle() in your myPhysListHadron class? Not doing so can cause vtable errors.

Looking at your original error message:
undefined reference to `vtable for G4LEKaonZeroInelastic'

and your code for that constructor in your OP:
3) Does G4LEKaonZeroInelastic have any pure virtual functions you haven't implemented? Not implementing a base class's pure virtual functions in your derived class will cause vtable errors.




Last edited on
Not implementing Having a pure virtual member function would make the class abstract
You would get an cannot declare variable ‘foo’ to be of abstract type ‘T’ error.


> virtual constructor
¿is there such a thing?
Last edited on
I am checking my classes for undefined virtual functions, I assume this will resolve the issue and will mark this thread as resolved, to be reopened if necessary.
Topic archived. No new replies allowed.