1
#include <iostream>

using namespace std;

class A{
public:
  int a;
  virtual void fun();
};

int main(){A obj;}

getting error undefined reference to 'vtable for A'. I want to know why without implement virtual function giving this type of error.

David V
  • 2,134
  • 1
  • 16
  • 22
user3049522
  • 107
  • 1
  • 7
  • 1
    Are you confusing virtual functions with *pure* virtual functions? – Frédéric Hamidi Feb 10 '15 at 09:33
  • 1
    Because that's how the language works. What are you trying to do? – n. m. could be an AI Feb 10 '15 at 09:34
  • http://stackoverflow.com/questions/2652198/difference-between-a-virtual-function-and-a-pure-virtual-function – Abhineet Feb 10 '15 at 09:35
  • 1
    @FrédéricHamidi Even if `fun` was a pure virtual function, the code would fail to compile as one can not instantiate abstract classes. – Some programmer dude Feb 10 '15 at 09:36
  • 2
    If you're asking why it's an error, that's because the language requires that all non-pure virtual destructors have definitions. If you're asking why you get that particular error message, see http://stackoverflow.com/questions/1693634 – Mike Seymour Feb 10 '15 at 09:37
  • @Joachim, absolutely. I was only wondering why the questioner is expecting a unimplemented function to leave the compiler unfazed. – Frédéric Hamidi Feb 10 '15 at 09:38
  • but it is fine look at http://ideone.com/ToorYk may be your compiler is very old? – Rupesh Yadav. Feb 10 '15 at 09:40
  • @RupeshYadav.: It's not fine. There's no requirement to diagnose the error, so the program might, or might not, compile, as long as it doesn't call the missing function. It looks like, with optimisation enabled, GCC eliminates the unused object, so that the missing vtable etc. aren't needed. – Mike Seymour Feb 10 '15 at 10:05
  • @MikeSeymour, Yes you are right, it is not working in vc++, see http://rextester.com/live/HEIK81240 – Rupesh Yadav. Feb 16 '15 at 10:27

1 Answers1

2

That is because you are declaring the function (whether it would be normal member function or virtual function) but you are not defining it anywhere..!!

You can try this way, so that it would compile and run fine.!

class A{ public:int a; virtual void fun(){}; };

int main(){ A obj; }
Shivaraj Bhat
  • 841
  • 2
  • 9
  • 20