链接器错误:未定义对“方形 vtable”的引用。代码包含虚函数
我检查了最常见的 未定义参考这里的vtable
问题,虽然这让我更好地理解了正在发生的事情,但我仍然无法收集足够的信息来弄清楚为什么我有这个错误。
我有一个简单的 Square 类,最初我试图从 Polygon 类继承它。由于我对 C++ 比较了解并且还在学习中,所以我还没有对多态性进行太多的实验。
不管怎样,在我尝试摆脱基类(Polygon
)之后,我认为这可能会有所帮助。不幸的是,我仍然遇到同样的错误,而且我不太知道发生了什么。我所知道的是,最初,Polygon
类需要一个至少包含构造函数定义的源文件,我确实给出了该定义。这消除了 Polygon
类的 vtable
错误。
我的主要问题是我仍然为 Square
类得到这个,它应该继承自 Polygon 类。我想知道的是如何正确实现这一点以避免出现vtable
错误,同时仍然获得多态性的好处?
代码
Polygon.h/.cpp
#ifndef POLYGON_H
#define POLYGON_H
#include "Shape.h"
#include "vector3f.h"
class Polygon
{
public:
Polygon();
virtual void Collide(Shape &s) = 0;
virtual void Collide(Polygon &p) = 0;
virtual bool Intersects(const Shape &s) = 0;
virtual bool Intersects(const Polygon &s) = 0;
protected:
virtual void Draw() const = 0;
};
#endif // POLYGON_H
//------------------
#include "Polygon.h"
Polygon::Polygon() {
}
*Square.h/.cpp
#ifndef SQUARE_H
#define SQUARE_H
#include "Polygon.h"
#include "Shape.h"
#include "Vec3f.h"
#include <QGLWidget>
class Square //: public Polygon
{
public:
Square(Vec2f lenwidth = Vec2f(), Vec3f color = Vec3f());
~Square();
virtual void Collide(Shape &s);
virtual void Collide(Square &s);
virtual void Collide(Polygon &p);
virtual bool Intersects(const Shape &s);
virtual bool Intersects(const Polygon &p);
virtual bool Intersects(const Square &s);
virtual float Area(void) const;
protected:
virtual void Draw();
private:
Vec2f mDimensions;
Vec3f mColor;
};
#endif // SQUARE_H
//----------------
#include "Square.h"
/**********/
/* PUBLIC */
/**********/
Square::Square(Vec2f lenwidth, Vec3f color) //: Polygon()
{
this->mDimensions = lenwidth;
this->mColor = color;
}
Square::~Square() {
}
void Square::Collide(Polygon &p) {
}
/************/
/* PRIVATE */
/************/
void Square::Draw() {
const int numPoints = mDimensions.X * mDimensions.Y;
glBegin(GL_LINE_STRIP);
glColor3f(mColor.X, mColor.Y, mColor.Z);
for (double i = 0; i <= numPoints; i++) {
const float x = mDimensions.X;
const float y = mDimensions.Y;
glVertex3f(x, y, 0.0);
mDimensions.X += 1;
mDimensions.Y += 1;
}
}
I've checked the most common undefined reference to vtable
question on here, and while that gave me a better understanding of what is going on, I still wasn't able to gather enough information to figure out why I'm having this error.
I have a simple Square class, which, originally I was trying to inherit from a Polygon class. Since I've been relatively knew to C++ and still learning, I haven't experimented with Polymorphism all that much.
Anyway, after I tried getting rid of the base class (Polygon
), I thought that might help things. Unfortunately, I'm still getting the same error, and I don't quite know what is going on. What I do know is that originally, the Polygon
class required a source file with at least a constructor definition, which I did give. That got rid of the vtable
error for the Polygon
class.
My main issue is that I'm still getting this for the Square
class, which was supposed to inherit from the Polygon class. What I'd like to know is how can I implement this properly to avoid getting a vtable
error while still getting the benefits of Polymorphism?
Code
Polygon.h/.cpp
#ifndef POLYGON_H
#define POLYGON_H
#include "Shape.h"
#include "vector3f.h"
class Polygon
{
public:
Polygon();
virtual void Collide(Shape &s) = 0;
virtual void Collide(Polygon &p) = 0;
virtual bool Intersects(const Shape &s) = 0;
virtual bool Intersects(const Polygon &s) = 0;
protected:
virtual void Draw() const = 0;
};
#endif // POLYGON_H
//------------------
#include "Polygon.h"
Polygon::Polygon() {
}
*Square.h/.cpp
#ifndef SQUARE_H
#define SQUARE_H
#include "Polygon.h"
#include "Shape.h"
#include "Vec3f.h"
#include <QGLWidget>
class Square //: public Polygon
{
public:
Square(Vec2f lenwidth = Vec2f(), Vec3f color = Vec3f());
~Square();
virtual void Collide(Shape &s);
virtual void Collide(Square &s);
virtual void Collide(Polygon &p);
virtual bool Intersects(const Shape &s);
virtual bool Intersects(const Polygon &p);
virtual bool Intersects(const Square &s);
virtual float Area(void) const;
protected:
virtual void Draw();
private:
Vec2f mDimensions;
Vec3f mColor;
};
#endif // SQUARE_H
//----------------
#include "Square.h"
/**********/
/* PUBLIC */
/**********/
Square::Square(Vec2f lenwidth, Vec3f color) //: Polygon()
{
this->mDimensions = lenwidth;
this->mColor = color;
}
Square::~Square() {
}
void Square::Collide(Polygon &p) {
}
/************/
/* PRIVATE */
/************/
void Square::Draw() {
const int numPoints = mDimensions.X * mDimensions.Y;
glBegin(GL_LINE_STRIP);
glColor3f(mColor.X, mColor.Y, mColor.Z);
for (double i = 0; i <= numPoints; i++) {
const float x = mDimensions.X;
const float y = mDimensions.Y;
glVertex3f(x, y, 0.0);
mDimensions.X += 1;
mDimensions.Y += 1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
vtable 链接错误的原因是您没有在类中定义第一个虚拟函数(即
Collide(Shape&p)
)。 (虚表通常与第一个虚函数定义一起存储)添加一个
void Square::Collide(Shape &p)
函数(只是一个空白的void Square::Collide(Shape & p) {}
应该可以工作)并且特定的 vtable 错误应该消失。请注意,您可能应该在标头中定义所有函数。
请记住,参数类型区分函数,就像 C++ 中的名称一样,
Collide(Shape)
与Collide(Polygon)
不同。The reason for the vtable linking error is that you are not defining the first virtual function in your class(which is
Collide(Shape&p)
). (vtables are usually stored along with the first virtual function definition)Add a
void Square::Collide(Shape &p)
function( just a blankvoid Square::Collide(Shape &p) {}
should work) and that specific vtable error should go away.Note that you are probably should define all the functions in your header.
Remember, arguments types differentiate functions just like names in C++, a
Collide(Shape)
is different from aCollide(Polygon)
.