如何在c ++中解决模糊的基类

时间:2014-02-02 20:36:29

标签: c++ class fltk

两个基类Arc和Lines都是从Shape类派生的。

编译器说Ojbect b1“错误:形状不明确”。我知道正在创建两个Shape实例,但不知道如何解决它?

Graph_lib::Box b1(Point,100,100), 100,100);
win1.attach(b1);

此类将能够绘制带圆角的框。我刚刚编写了Box Lines部分的代码,我还没有进入Arc,因为这甚至都不起作用。

//------------------------------------------------------------------------------

struct Box : Lines , Arc {

     Box(Point xy, int ww, int hh); 

     void Top_segment();
     void Bottom_segment();
     void Left_side_segment();
     void Right_side_segment();

    void draw_lines() const;

    int height() const { return h; }
    int width() const { return w; }
private:
    int h;    // height
    int w;    // width

    double width_tenth; //10% of the width that will calculate the length to remove from each side to make room for the arcs

    };

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
    Box::Box(Point xy, int ww, int hh): w(ww), h(hh) 
    {

        width_tenth = (xy.x + w) * 0.10;

        if (h<=0 || w<=0) error("Bad box: non-positive side");

    }
//------------------------------------------------------------------------------
 void Box::Top_segment()
 {

    double top_seg_begin_w;   //where the line segment will begin after deducting 10% of w;
    double top_seg_end_w;     //where the line segment will end after deducting 10% of w;

        top_seg_begin_w = xy.x + width_tenth;

        top_seg_end_w = (xy.x + w) - width_tenth;

        Lines::add(Point(top_seg_begin_w,xy.y),Point(top_seg_end_w,xy.y));
 }

//------------------------------------------------------------------------------
 void Box::Bottom_segment()
 {

    double bottom_seg_begin_w;  
    double bottom_seg_end_w;

        bottom_seg_begin_w = xy.x + width_tenth;

        bottom_seg_end_w = (xy.x + w) - width_tenth;

        double y_bottom = xy.y + h;

        Lines::add(Point(bottom_seg_begin_w,y_bottom),Point(bottom_seg_end_w,y_bottom));
 }

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
 void Box::Left_side_segment()
 {
        double left_seg_begin_h;
        double left_seg_end_h;  

        left_seg_begin_h = xy.y + width_tenth;

        left_seg_end_h = (xy.y + h) - width_tenth;

        double x_left = xy.x;

        Lines::add(Point(x_left,left_seg_begin_h),Point(x_left,left_seg_end_h));
 }

//------------------------------------------------------------------------------
 void Box::Right_side_segment()
 {
        double right_seg_begin_h;
        double right_seg_end_h;     

        right_seg_begin_h = xy.y + width_tenth;

        right_seg_end_h = (xy.y + h) - width_tenth;

        double x_right = xy.x + w;

        Lines::add(Point(x_right,right_seg_begin_h),Point(x_right,right_seg_end_h));
 }

//------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

对LINE和Arc类使用虚拟继承。例如

class Lines : virtual public Shape
{
//...
};

class  Arc : virtual public Shape
{
//...
};