Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined vtable C++

Tags:

c++

g++

#include <stdio.h>

class A {
public:
  virtual void print();
  A();
};

class B :public A {
public:
  void print();
  B();
};

class C :public B {
public:
  void print();
  C();
};

A::A(){
}

B::B(){
}

C::C(){
}

void B::print() {
  printf("From B\n");
}

void C::print() {
  printf("From C\n");
}

int main() {
  B* object = new C;
  object->print();

  return 0;
}

When I try to compile this C++ file, I get the following error. Can't figure out the reason. I tried reading through similar undefined vtable questions on SO.

/tmp/ccpOkVJb.o: In function `A::A()':
test1.cpp:(.text+0xf): undefined reference to `vtable for A'
/tmp/ccpOkVJb.o:(.rodata._ZTI1B[_ZTI1B]+0x10): undefined reference to `typeinfo for A'
collect2: error: ld returned 1 exit status
like image 256
yasith Avatar asked Dec 18 '25 10:12

yasith


1 Answers

If A::print() isn't meant to be implemented, declare it as pure:

class A {
public:
  virtual void print() = 0;
  A();
};

Otherwise, implement it.

like image 79
Luchian Grigore Avatar answered Dec 21 '25 02:12

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!