2013-04-23 80 views
0

我收到标题中指出的错误消息。我试图构造一个继承Shape类的类Line。在行构造函数中执行构造函数中的“对行中的vtable引用未定义”

Shape(color) {} 

错误。

头文件(形状和线条于一身,颜色不同)

Shape.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 

image.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){ 
} 
+0

也许添加颜色(颜色&)构造函数结构颜色? – 2013-04-23 21:15:26

+0

那也行不通 – user1784297 2013-04-23 21:33:43

回答

0

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

+0

你会怎么做这样的事情? – user1784297 2013-04-23 21:21:56

+0

通过将'颜色'[上面形状](http://coliru.stacked-crooked.com/view?id=cda018b999e446bb39a1f208ca095ba4-50d9cfc8a1d205ca7409e81e87c2653ba) – alexrider 2013-04-23 21:23:59

+0

但颜色是在“Image.h”。 “Image.h”包含在“Shape.h”中。这不够吗? – user1784297 2013-04-23 21:25:33

0

似乎从Line类的Draw声明中收回了“void”。由于某种原因