2

The problem says: Draw a box with rounded corners. Define a class Box, consisting of four lines and four arcs. So I wrote the below code for that exercise:

#include <Simple_window.h>
Simple_window win(Point(100,100), 600,400, "semi-ellipse");

struct Box: Shape{

    Box(Point p, int ww, int hh): w(ww), h(hh) 
        { add(Point(p.x-ww,p.y-hh));  }

    void d_l() const        //creating 4 lines
     {
        Line hrz1 (Point(150,100), Point(400,100));
        Line hrz2 (Point(150,300), Point(400,300));
        Line ver1 (Point(507,150), Point(507,250));
        Line ver2 (Point(41,150), Point(41,250));

        win.attach(hrz1);
        win.attach(hrz2);
        win.attach(ver1);
        win.attach(ver2);
     }

    void draw_lines() const      //creating 4 arcs
    {
        fl_arc(point(0).x,point(0).y,w,h,30,90);
        fl_arc(point(0).x,point(0).y,w,h,270,330);
        fl_arc(point(0).x,point(0).y,w,h,90,150);
        fl_arc(point(0).x,point(0).y,w,h,210,270);
    }

private:
    int w;
    int h;
};

int main()
{
    using namespace Graph_lib; 

    Box b(Point(100,100),100,50);
    win.attach(b);
    win.wait_for_button();
}

When I ran it I faced this exception:

Unhandled exception at 0x757FE9D7 (ole32.dll) in test.exe: 0xC0000005: Access violation reading location 0x00000004.

I know this refers to declaring Simple_window win(Point(100,100), 600,400, "semi-ellipse"); in global state. But I did that because I had to do it. The problem is that how to attach lines and also object (here b) to Simple_window win in either parts (main() function and also Box struct).

Nekresh
  • 2,948
  • 23
  • 28
zhiar
  • 113
  • 9
  • No reply from those numerous C++ experts!!? – zhiar May 08 '14 at 11:58
  • Without the Simple_window source we simply can't help you. Point and Shape are also classes that come from somewhere else, not FLTK. – DejanLekic May 08 '14 at 15:22
  • I know what you want to do but please wait a menuite. I haven't been taought to read the source of those files!!! Yes. What I have been taought is just to use them so I should solve this problem. What I need from you is to read the context of the exercise and check do I have understood the problem correctly and if so, so please give if you have any other solution. Thanks. – zhiar May 08 '14 at 19:35
  • Suggest you learn to use the debugger and find out where you program is falling over or at least tell us what the call stack looks like if you can't understand it. – cup May 09 '14 at 18:14
  • My program is falling over just from the line _Simple window_ (second line of the code). Cube, don't go far, the answer is near. If you can tell how to use the _win_ in two bodies (here _main()_ and _Box_ class), the problem will be solved. Just this case. – zhiar May 10 '14 at 05:57
  • What is Simple_Window? We can't tell you how to do anything if we don't know what you are playing with. – cup May 11 '14 at 22:31
  • Simple_Window is not from FLTK. It is from Bjarne S. book... Do not spread false information. – DejanLekic May 13 '14 at 13:38
  • OK. I did that small mistake. I could to find the problem of this code. If you like to help please have a look at http://stackoverflow.com/questions/23625031/how-to-send-a-rectangle-to-a-function-which-takes-a-rectangle-argument-in-c/23625553?noredirect=1#comment36286284_23625553 – zhiar May 13 '14 at 13:46

3 Answers3

1

Looks like it is caused by the global creation of win. I have never run an FLTK program where anything graphical is created before main but I'm guessing that sometimes the graphics libs require some things to be in place so it is best to use them after main.

What can you do about it? If win is declared as a pointer and created inside main instead of outside main, then you won't get a crash.

...
Simple_window* win;
struct Box: Shape
{
  ...
   win->...
   ...
}


int main()
{
    win = new Simple_window(Point(100, 100), 600, 400, "semi-ellipse");
    Box b ...
    win->attach ...
    win->wait ...
    delete win;
}
cup
  • 7,589
  • 4
  • 19
  • 42
1

You don't need to put anything in global space at all. You want to be passing information to the classes that need them as opposed to needing everything declared in global scope before you do anything with it.

What I write here modifies your Box class so that it also has a private member variable which is a pointer to a Simple_window class. The constructor is modified so that when you construct Box b you have to send it a pointer to the Simple_window in which it will be drawn. When you do this the pointer is assigned the pointer to the then declared Simple_Window win.

#include <Simple_window.h>


struct Box: Shape{

private:
    int w;
    int h;
    Simple_window* window;
public:

    Box(Point p, int ww, int hh,Simple_window* win_): w(ww), h(hh),window(win_)
    { add(Point(p.x-ww,p.y-hh));  }

    void d_l() const        //creating 4 lines
    {
        Line hrz1 (Point(150,100), Point(400,100));
        Line hrz2 (Point(150,300), Point(400,300));
        Line ver1 (Point(507,150), Point(507,250));
        Line ver2 (Point(41,150), Point(41,250));

        window->attach(hrz1);
        window->attach(hrz2);
        window->attach(ver1);
        window->attach(ver2);
    }

    void draw_lines() const      //creating 4 arcs
    {
        fl_arc(point(0).x,point(0).y,w,h,30,90);
        fl_arc(point(0).x,point(0).y,w,h,270,330);
        fl_arc(point(0).x,point(0).y,w,h,90,150);
        fl_arc(point(0).x,point(0).y,w,h,210,270);
    }


};

int main()
{
    using namespace Graph_lib;
    Simple_window* win = new Simple_window(Point(100,100), 600,400, "semi-ellipse");
    Box b(Point(100,100),100,50,win);
    win->attach(b);
    win->wait_for_button();
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49
user3353819
  • 911
  • 2
  • 8
  • 21
0

Your lines are hard coded and independent of the constructor parameters. They could be drawn using directly the FLTK library, similarly with the arcs and placed inside the inherited from class Shape and overriden function void draw_lines() const. Then you don't need the window object to be pointer.

A possible implementation is:

Box::Box(Point p, int w, int h)
    : width(w), height(h)
{ 
    add(Point(p.x - w, p.y - h));
}

void Box::draw_lines() const
{
    // draw lines with reduced length to adapt for the arcs
    if (color().visibility())
    {
        // upper horizontal
        fl_line(point(0).x + width/4, point(0).y, point(0).x + (3./4.) * width, point(0).y);
        // lower horizontal
        fl_line(point(0).x + width/4, point(0).y + height, point(0).x + (3./4.) * width, point(0).y + height);
        // left vertical 
        fl_line(point(0).x, point(0).y + height/4, point(0).x, point(0).y + (3./4.)*height);
        // right vertical 
        fl_line(point(0).x + width, point(0).y + height/4, point(0).x + width, point(0).y + (3./4.) * height);
    }

    // draw arcs
    if(color().visibility())
    { 
        fl_color(fill_color().as_int());
        // upper left arc
        fl_arc(point(0).x, point(0).y, width/2, height/2, 90, 180); 
        // upper right arc
        fl_arc(point(0).x + width/2, point(0).y, width/2, height/2, 0, 90);
        // down right arc
        fl_arc(point(0).x + width/2, point(0).y + height/2, width/2, height/2, 270, 0); 
        // down left arc
        fl_arc(point(0).x , point(0).y + height/2, width/2, height/2, 180, 270); 
    }
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49