C++ Undefined Reference to vtable and inheritance
Why the error & how to resolve it?
You need to provide definitions for all virtual functions in class A
. Only pure virtual functions are allowed to have no definitions.
i.e: In class A
both the methods:
virtual ~A();
virtual void doWork();
should be defined(should have a body)
e.g.:
A.cpp
void A::doWork()
{
}
A::~A()
{
}
Caveat:
If you want your class A
to act as an interface(a.k.a Abstract class in C++) then you should make the method pure virtual.
virtual void doWork() = 0;
Good Read:
What does it mean that the "virtual table" is an unresolved external?
When building C++, the linker says my constructors, destructors or virtual tables are undefined.
Undefined reference to vtable
So, I've figured out the issue and it was a combination of bad logic and not being totally familiar with the automake/autotools world. I was adding the correct files to my Makefile.am template, but I wasn't sure which step in our build process actually created the makefile itself. So, I was compiling with an old makefile that had no idea about my new files whatsoever.
Thanks for the responses and the link to the GCC FAQ. I will be sure to read that to avoid this problem occurring for a real reason.
Undefined Reference to vTable for constructor and destructor
Two things:
1) This code is very unlikely to be what you want:
class MinHeap : virtual public AbstractHeap
virtual derivation is only of use when you are doing multiple inheritance (which you aren't here, from what you've shown), and even then it's best avoided.
You simply need:
class MinHeap : public AbstractHeap
I suspect this causes the error.
2) If you have a base class, its destructor should be declared as virtual
, so:
virtual ~AbstractHeap();
If not, then when you delete
a derived class object, the derived class destructor may be skipped. So you need a virtual
destructor in the base class even if it does nothing and has an empty body, because the benefit is in the derived class.
c++ inheritance issues: undefined reference to 'vtable'
As pointed out in the comments, by calling g++ main.cpp
you are only compiling main.cpp
.
You need to compile all files, then link them together. If you do so, you will see that there are compilation issues in your other cpp files as also pointed out in the comments (virtual and override only belong in the header).
So you need to call the following to compile all files:
g++ main.cpp Base.cpp Derived.cpp -o myapp
undefined reference to vtable with inheritance
You do not define the virtual string ManagedObjects::get() const
method anywhere. Either define it as a pure virtual function virtual string get() const = 0;
or provide a declaration for it.
Undefined reference error while using inheritance and pure virtual functions
Both inheritance and the presence of pure virtual functions are not the problem. You'd get similar error for
class rectangle {
public:
rectangle();
void display();
float area(){
int A = 42;
return A;
}
};
int main(){
rectangle rec;
rec.display();
}
You declare a constructor and you declare a method display
and the compiler takes that as a promise that you will provide a definition of them somewhere. The linker cannot find them, hence the error. You have to define those methods.
However, in your code actually both methods are not needed. You can use the compiler generated constructor and you can reuse shape::display
, hence you can change rectangle
to:
class rectangle : public shape{
public:
float area(){
int A;
A = width*height;
return A;
}
};
undefined reference to `vtable for ' namespace inheritance undefined reference to `typeinfo for
The problem is, that you have to define every function you declare. That's why the compiler is complaining, you are not defining anything for draw
, move
or scale
.
If you want Figure
to be a complete abstract class (which I think this is what you are trying to do), you can set those function to 0
, so you don't have to define them, but the derived classes have to implement them.
//Now you don't need to implement them
virtual void draw() = 0;
virtual void move() = 0;
virtual void scale(double) = 0;
undefined reference to vtable for inheriting classes
As explained at https://gcc.gnu.org/wiki/VerboseDiagnostics#missing_vtable the vtable will be in the same object file as the first non-inline virtual function, which is Layer::start
. You haven't defined that function, so the compiler never generated the vtable.
To fix the linker error be sure you have provided a definition for the first non-inline virtual function declared in the class.
Undefined reference to vtable
You need to either make virtual void move();
a pure virtual function:
virtual void move() = 0;
or define Actor::move()
for a base class
void Actor::move()
{
// do something
}
Related Topics
"Inline" Keyword VS "Inlining" Concept
Why Does Libc++'s Implementation of Std::String Take Up 3X Memory as Libstdc++
How to Use Covariant Return Types with Smart Pointers
Compilation Error: "Stddef.H: No Such File or Directory"
Isn't the Const Modifier Here Unnecessary
How to Initialize Std::Vector Over Already Allocated Memory
Why Does the Most Negative Int Value Cause an Error About Ambiguous Function Overloads
How to Return in Void Function
Passing Rvalues Through Std::Bind
Inferring the Call Signature of a Lambda or Arbitrary Callable for "Make_Function"
Cancelling Boost Asio Deadline Timer Safely
I Want to Convert Std::String into a Const Wchar_T *
Most Efficient/Elegant Way to Clip a Number