第一个代码块工作,但第二个代码块没有,我不知道为什么。我在第二段代码中遇到了分段错误。
#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]);
}
}
以下代码给出了分段错误。
#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();
}
我已将问题缩小到“win.attach(weekdays [weekdays.size() - 1])”这一行,但除此之外,我感到难过。