Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对于循环分段——过度运行(i_C++_Linux_User Interface_For Loop_Gnome - Fatal编程技术网

C++ 对于循环分段——过度运行(i

C++ 对于循环分段——过度运行(i,c++,linux,user-interface,for-loop,gnome,C++,Linux,User Interface,For Loop,Gnome,这是一个复制Sierpinski垫圈的程序中的函数。该函数用于为分形附加三角形中的点 经过深思熟虑,我发现问题出在哪里: void add_pts(int &x, int &y) { Vector_ref<Rectangle> pt; for (int i = 0; i < POINTS; ++i) { pt_test; //generates changing x, y vals within the limits

这是一个复制Sierpinski垫圈的程序中的函数。该函数用于为分形附加三角形中的点

经过深思熟虑,我发现问题出在哪里:

void add_pts(int &x, int &y)
{ 
    Vector_ref<Rectangle> pt;
    for (int i = 0; i < POINTS; ++i)
    {
         pt_test; //generates changing x, y vals within the limits of the triangle
         cout << "pass" << i <<endl;
         pt.push_back(new Rectangle(Point(x,y),5,5));
         pt[i].set_fill_color(Color::yellow);
         win.attach(pt[i]); 
    }
}
输出为pass1…pass[POINTS-1],但无论出于何种原因,它在i=POINTS时运行,并遇到分段错误。我不知道为什么。有人能帮忙吗

这是我的密码。pt_测试和coord有点草率,但鉴于它不能正常运行,很难确定我能简化哪些

#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <time.h>
#include "Simple_window.h"
#include "Graph.h"
#include "Point.h"
#include "GUI.h"
#include "Window.h"
using namespace std;
using namespace Graph_lib;

// globals

const int POINTS = 5000;
unsigned int seed = (unsigned int)time(0);
Simple_window win(Point(100,100),1100,700,"Homework 9");

// function declarations

double random(unsigned int &seed);
bool coords(int &x, int &y);
void pt_test(int x, int y);
void add_pts(int &x, int &y);

int main()
{

    int x, y;

    // title

    Text title(Point(400,50), "The Sierpinski Gasket");
    title.set_font(Graph_lib::Font::helvetica_bold);
    title.set_font_size(25);
    title.set_color(Color::cyan);
    win.attach(title);

    // triangle

    Closed_polyline tri;
    tri.add(Point(250,75)); // A
    tri.add(Point(850,75)); // B
    tri.add(Point(550,675)); // C
    tri.set_fill_color(Color::white);
    tri.set_color(Color::dark_red);
    tri.set_style(Line_style(Line_style::solid,3));
    win.attach(tri);

    // vertices
    Text vert_a(Point(225,70), "A (250, 75)");
    vert_a.set_font(Graph_lib::Font::helvetica_bold);
    vert_a.set_font_size(15);
    vert_a.set_color(Color::cyan);
    Text vert_b(Point(855,70), "B (850, 75)");
    vert_b.set_font(Graph_lib::Font::helvetica_bold);
    vert_b.set_font_size(15);
    vert_b.set_color(Color::cyan);
    Text vert_c(Point(575,670), "C (550, 675)");
    vert_c.set_font(Graph_lib::Font::helvetica_bold);
    vert_c.set_font_size(15);
    vert_c.set_color(Color::cyan);

    win.attach(vert_a);
    win.attach(vert_b);
    win.attach(vert_c);

    // point selection

    add_pts(x, y);

    // window title and display

    win.wait_for_button();

}

double random(unsigned int &seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double(seed)/double(MODULUS);
}

bool coords(int &x, int &y) // generates the points
{
    x = int(251 + 600*random(seed));
    y = int(76 + 600*random(seed));
    if( y > (2*x-425) && x<= 550 || x>=550 && y < (-2*x + 1775))
        return true;
}

void pt_test(int x, int y) // tests the points until they are within the range
{
    coords;
    while(coords == 0)
        coords;
}

void add_pts(int &x, int &y) // attaches the points as shapes
{ 
    Vector_ref<Rectangle> pt;
    for (int i = 0; i < POINTS; ++i)
    {
         pt_test;
     cout << "i == " << i << "  points == " << POINTS << endl;
         pt.push_back(new Rectangle(Point(x,y),5,5));
         pt[i].set_fill_color(Color::yellow);
         win.attach(pt[i]); 
    }
}

我还注意到,当主体在循环中时,函数add_pts不起作用,但是如果将主体放在int_main中,它会无限期地运行,但不会很快到达分段错误(如果有的话)。

可能还会在该行上输出点?也许它的值与您认为的不同?pt_测试是宏吗?它是如何定义的?同样的问题是分数。你确定i=某个点的分数,并且它不会因为不同的原因崩溃吗?你说输出是pass1…pass[POINTS-1]。还有,输出是从1开始的吗?我们需要一个MCVE。这段代码中没有理由自行中断。什么是向量?你是干什么的?