【问题标题】:"Undefined reference to vtable in Line "in constructor构造函数中的“未定义对行中 vtable 的引用”
【发布时间】:2013-04-23 21:09:44
【问题描述】:

我收到标题中所述的错误消息。我正在尝试构建一个继承了 Shape 类的 Line 类。我在

中遇到错误
Shape(color) {}

在行构造函数中执行。

头文件(形状和线条在一个,颜色在另一个)

形状.h:

#ifndef SHAPE
#define SHAPE

#include "Image.h"
class Shape{
private:
    Color color;
public:
    Shape (const Color & col) : color(col){}
    virtual void Draw(Image & figure) const = 0;

};

class Line : public Shape{
public:
    Line (int x1, int y1, int x2, int y2, const Color & color);
    virtual void Draw(Image & figure) const;

private:
    int x1, y1, x2, y2;
};
#endif // SHAPE

图像.h:

struct Color {
    Color( unsigned char r, unsigned char g, unsigned char b ) 
    : red( r ), green( g ),     blue( b ) {
    }
    Color() : red( 0 ), green( 0 ), blue( 0 ) {
    }
    unsigned char red, green, blue;
};

这里是 Shape.cpp

#include "Shape.h"

Line::Line( int x1, int y1, int x2, int y2, const Color &color )
    : x1( x1 )
    , x2( x2 )
    , y1( y1 )
    , y2( y2 )
    , Shape(color){
}

【问题讨论】:

  • 也许在 struct Color 中添加一个 Color(Color&) 构造函数?
  • 那也没用

标签: c++ inheritance vtable


【解决方案1】:

struct Color 必须在用作Shape 的成员之前声明。

【讨论】:

  • 这样的事情怎么办?
  • 通过输入Color above Shape
  • 但颜色在“Image.h”中。 “Image.h”包含在“Shape.h”中。这还不够吗?
  • 是标题:"undefined reference to `vtable for Line'" in , Shape (color) {
  • 看起来 Shape.cpp 没有与其余代码编译/链接。检查你的构建日志。
【解决方案2】:

似乎从 Line 类的 Draw 声明中收回“void”是可行的。不知为何

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2020-07-07
    • 2015-05-25
    • 2014-06-05
    • 2016-09-26
    • 2012-08-18
    相关资源
    最近更新 更多