调用fl_line()时出现FLTK SIGSEGV分段错误 [英] FLTK SIGSEGV Segmentation Fault when calling fl_line()

查看:139
本文介绍了调用fl_line()时出现FLTK SIGSEGV分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中调用fl_line()在屏幕上绘制1条线后立即返回SIGSEGV异常。我已经检查了向量偏移量,没有任何限制:

Calling fl_line() in my program immediately returns an SIGSEGV exception after drawing 1 line on the screen. I've checked vectors offset, nothing out of bounds:

这里是崩溃时的样子

这里是调用堆栈

为什么用参数而不是Fl_graphics_driver :: line()调用fl_line()?

Why is fl_line() called with parameters but not Fl_graphics_driver::line() ?

这是我的程序,它是从4个点中绘制一个简单的多边形:

Here's my program, it was to draw a simple polygon out of 4 points:

#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());
}


推荐答案

颜色图尚未组。设置颜色会初始化颜色图。一个好地方就是MyWindow构造函数。

The colour map has not been set. Setting the colour initializes the colour map. A good place to put it would be in the MyWindow constructor.

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);
}

在FL / Enumerations中可以找到颜色列表。H

A list of colours can be found in FL/Enumerations.H

这篇关于调用fl_line()时出现FLTK SIGSEGV分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆