1

I've started learning C++, I know C and Java already. I've started learning it because I want to start using object oriented programming.

However, I am stuck with code because compiler generates "undefined reference to vtable for Actor". Here you have code that generates same error, not the original one tho, because it would be less clear. I have really no idea what causes it.

struct Actor
{
     int x, y;
     virtual void move();
};

struct Player : Actor
{
     Player(int a, int b)
     {
        x = a;
        y = b;
     }

     void move();
     void draw();
};

void Player::move()
{
    ++x;
};

main()
{
    Actor *act;

    act = new Player(10, 20);
}

This question may be dumb, I don't know, I've dug everywhere but found nothing that would solve my problem.

pampeho
  • 704
  • 6
  • 12

5 Answers5

9

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
}
Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53
2

In Actor define

virtual void move() = 0;

instead of

virtual void move();
SiimKallas
  • 934
  • 11
  • 23
0

The error undefined reference to vtable usually means that a virtual function is declared but not defined.

0

@Maxim provided the answer in your case, however more generally speaking (from the link provided by @Maxim):

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual.

What brought me to a grinding halt is the last sentence in the above quote, which is not obvious at first glance.

pauluss86
  • 454
  • 6
  • 9
0

For every class with virtual functions, an entry for class in made in vtable which further has pointers to all the virtual functions.

since there occurs a virtual void move(); declaration in base class, compiler looks for the class definition(i.e) a corresponding entry in vtable whoch is not available here as u have not defined .

so pure virtual functions will resolve the err.

Madhu
  • 61
  • 1
  • 9