0

the first block of code works, but the second block of code doesn't and I have no idea why. I'm getting a segmentation fault with the second block of code.

#include "Graph.h"
#include "Simple_window.h"
#include "std_lib_facilities_3.h"

using namespace Graph_lib;

Point t1(100,100);
Simple_window win(t1,700,700,"Calendar");

int main(){
    Vector_ref<Text> weekdays;
    vector<string> weekday = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    for (int a = 0; a < weekday.size(); ++a){
        weekdays.push_back(new Text(Point(a*100+28,90), weekday[a]));
        weekdays[weekdays.size()-1].set_font_size(10);
        win.attach(weekdays[weekdays.size()-1]);
    }
}

The following code gives me a segmentation fault.

#include "Graph.h"
#include "Simple_window.h"
#include "std_lib_facilities_3.h"

using namespace Graph_lib;

Point t1(100,100);
Simple_window win(t1,700,700,"Calendar");

void draw_weekdays(){
    Vector_ref<Text> weekdays;
    vector<string> weekday = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    for (int a = 0; a < weekday.size(); ++a){
        weekdays.push_back(new Text(Point(a*100 + 28, 90), weekday[a]));
        weekdays[weekdays.size()-1].set_font_size(10);
        win.attach(weekdays[weekdays.size()-1]);
    }
}

int main(){
    draw_weekdays();
}

I've narrowed down the issue to the line "win.attach(weekdays[weekdays.size()-1]);", but other than that I'm stumped.

Jonas
  • 6,915
  • 8
  • 35
  • 53
David
  • 7,028
  • 10
  • 48
  • 95

0 Answers0