对 vtable 的未定义引用 [英] Undefined reference to vtable

查看:60
本文介绍了对 vtable 的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习 C++,我已经知道 C 和 Java.我开始学习它是因为我想开始使用面向对象编程.

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.

但是,我被代码困住了,因为编译器会生成对 Actor 的 vtable 的未定义引用".在这里,您有生成相同错误的代码,而不是原始错误,因为它不太清楚.我真的不知道是什么原因造成的.

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.

推荐答案

您需要要么使 virtual void move(); 成为纯虚函数:

You need to either make virtual void move(); a pure virtual function:

virtual void move() = 0;

或为基类定义Actor::move()

void Actor::move() 
{
    // do something
}

这篇关于对 vtable 的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆