2017-09-28 5 views
-1

私のプログラムでfl_line()を呼び出すと、画面上に1行を描画した直後にSIGSEGV例外が返されます。私はベクトルがオフセットチェックしました、範囲外のもの:FLTK SIGSEGV fl_line()を呼び出すときのセグメンテーションフォルト

Here's how it looks like when it crashes

Here's the call stack

なぜfl_lineではありません()のパラメータで呼び出されますがFl_graphics_driverない::ライン()?

はここに私のプログラムですが、それは、4点のうち、簡単なポリゴンを描画することでした:

#include <iostream> 

#include <FL/Fl.H> 
#include <FL/Fl_draw.H> 
#include <FL/Fl_Window.H> 
#include <FL/Fl_Widget.H> 
#include <initializer_list> 
#include <vector> 
#include <functional> 
//#include <cmath> 
//#include <math.h> 

struct Point { 
int x,y; 
Point(int xx, int yy) : x(xx), y(yy) { } 
}; 


class Shape: public Fl_Widget{ 
public: 

Shape(int x, int y, int w, int h) : Fl_Widget(x,y,w,h) {} 

Point point(int idx) const { 
return (points[idx]); 
} 
unsigned int points_size() const { 
return points.size();} 

void draw() override{ 
draw_lines(); 
} 
void line(int x, int y, int x1, int y1) const {fl_line(x,y,x1,y1);} 
void add(Point p){ points.push_back(p); } 

protected: 
virtual void draw_lines() const{} 

private: 
std::vector<Point> points; 
}; 


class ClosedPolyline: public Shape { 
public: 
/*ClosedPolyline(std::initializer_list<Point> pp) { 
if (pp.size() > 0) { 
for (Point p: pp) 
add(p); 
} 
} 
*/ 
ClosedPolyline(Point a1, Point a2, Point a3, Point a4) : Shape(a1.x,a1.y,a3.x-a1.x,a2.y-a1.x) { 
    add(a1); add(a2); add(a3); add(a4); 
} 

protected: 

void draw_lines() const override{ 

for (unsigned int i=1; i<points_size(); ++i){ 
    std::cout << point(i-1).x << " " << point(i-1).y << " to " << point(i).x << " " << point(i).y << std::endl; 
} 
/* 
for (unsigned int i=1; i<points_size(); ++i){ 
fl_line(point(i-1).x, point(i-1).y, point(i).x, point(i).y); 
} 
*/ 

line(point(0).x, point(0).y, point(1).x, point(1).y); 
line(point(1).x, point(1).y, point(2).x, point(2).y); 
line(point(2).x, point(2).y, point(3).x, point(3).y); 
line(point(1).x, point(1).y, point(2).x, point(2).y); 
} 
}; 



class MyWindow: public Fl_Window { 
public: 
MyWindow(int x, int y, int w, int h, const char* title = 0) 
: Fl_Window(x, y, w, h, title) {} 
void Attach(Shape &s) { 
shapes.push_back(&s); 
//shapes.push_back(s); 
//draw(); 
} 
//void draw_shapes(){draw();} 
protected: 
void draw() override{ 
for(Shape * s: shapes) { 
s->draw(); 
//s.draw(); 
} 
} 

private: 
std::vector<Shape*> shapes; 
}; 

/* 
class Circle: public Shape { 
public: 
Circle(Point p, double r): radius(r) { 
add(Point{p.x - r, p.y - r}); 
} 

protected: 
void draw_line() { 
fl_arc(point(0).x, point(0).y, radius + radius,radius + radius, 0, 360); 
} 

private: 
double radius; 
}; 

void Function(T f, double r1, double r2, 
Point xy, int count = 100, double xscale = 25, double yscale = 25) { 
double dist = (r2-r1)/count; 
double r = r1; 
for (int i = 0; i<count; ++i) { 
add(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale))); 
r += dist; 
} 
} 
}; 

class CosFunction { 
public: 
double (double x) { 
return cos(x * M_PI/180); 
} 
}; 
*/ 

typedef double Fct(double); 

int main() { 
MyWindow win(100, 100, 600, 400, "C++ Test task"); 

ClosedPolyline p{Point{100, 100}, Point{100, 200}, Point{500, 100}, Point{500, 200}}; 
/* 
Function<std::function<double(double)>> f1 {[] (double x) -> double { return x * x; }, -100, 100, Point {300, 300}, 100, 20, 5}; 
Function<Fct> f2 {sin, -360, 360, Point{300, 300}, 200, 1, 25}; 
Function<CosFunction> f3{CosFunction(), -360, 360, Point{300, 300}, 200, 1, 25}; 
Circle c1{Point{300, 50}, 30}; 
*/ 

win.Attach(p); 
/* 
win.Attach(f1); 
win.Attach(f2); 
win.Attach(f3); 
win.Attach(c1); 
*/ 

win.end(); 

win.show(); 
return (Fl::run()); 
} 

答えて

0

カラーマップが設定されていません。色を設定すると、カラーマップが初期化されます。 MyWindowのコンストラクタに置くのが良い場所です。

MyWindow(int x, int y, int w, int h, const char* title = 0) 
    : Fl_Window(x, y, w, h, title) { 

    fl_color(FL_FOREGROUND_COLOR); 
} 

色のリストは

+0

おかげFL/Enumerations.Hで見つけることができます。それはすべてを修正します。 –

関連する問題