Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

c++ - Undefined reference to vtable

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...