Error "Undefined reference to vtable" in C++: causes and solution
Hello! I'm writing a small project in C++ using polymorphism. I have an abstract base class `Shape` and several derived classes: `Circle`, `Rectangle`. In the base class, a virtual function `area()` is declared. When I try to compile the project with g++, the compiler gives an error:
```
undefined reference to 'vtable for Shape'
```
Here's the simplified code:
shape.h:
```cpp
#ifndef SHAPE_H
#define SHAPE_H
class Shape {
public:
virtual double area() = 0; // pure virtual function
virtual ~Shape() {}
};
#endif
```
circle.h:
```cpp
#include "shape.h"
class Circle : public Shape {
public:
Circle(double r);
double area() override;
private:
double radius;
};
```
circle.cpp:
```cpp
#include "circle.h"
Circle::Circ
No answers yet.
Log in to leave an answer