我不确定我接近这个的方式是否比它必须更复杂。如果您有任何想法,请推荐更好的选择。我正在尝试创建一系列节点(窗口),每个节点都包含一串备用节点(框架结构中的可视信息)。
以下是我的调试器突出显示的行:
temp->obj_tail->next = n;
// temp is a node, and temp->obj_tail dereferences a node of the same struct
我得到了回复:“程序收到信号SIGSEGV,分段错误。”
我使用此构造函数将head,tail和temp初始化为NULL
cmenu::cmenu():
head(NULL),
tail(NULL),
temp(NULL)
{
}
调用类函数的主函数
cmenu Menu;
Menu.addWindow("side_bar");
Menu.addWindow("home");
Menu.addWindow("help");
//Box structs defined here, code omitted.
Menu.addBox("side_bar", "box_1", box1);
Menu.addBox("home", "box_2", box2);
Menu.addBox("help", "box_3", box3);
.h
中的相应结构struct box
{
int x; int y; int z;
int w; int h;
int fill_color; int fill_char;
int text_color; std::string text;
bool enabled;
box():
x(0),y(0),z(0),
w(0), h(0),
fill_color(8), fill_char(32), enabled(true),
text_color(15), text(""){}
};
class cmenu
{
private:
typedef struct node
{
node* next; // Next node
node* prev; // Prev node
node* obj_head; // Head of other node stack
node* obj_tail; // Tail of other node stack
bool enabled; // Allows toggle of element
std::string id; // Used to ID nodes
box box; // Visual element
} *nodePtr;
nodePtr head;
nodePtr tail;
nodePtr temp;
// class functions here, code omitted.
};
发生错误的类.cpp中的函数
void cmenu::addBox(std::string win_id, std::string box_id, box b)
{
temp = head;
while (temp->id != win_id) {
if (temp != tail) {
temp = temp->next;
}
}
// At this point the program has found the node with the id of
// win_id. And now my goal is to connect a new and separate
// string of nodes to the obj_head and obj_tail.
// This can only happen if there has already been a node connected
// which is what the else statement does when temp->obj_tail is NULL
// so this shouldn't be happening
if (temp->id == win_id) {
nodePtr n = new node;
n->id = box_id; // The id is assigned
n->box = b; // The box struct is set/copied to the passed parameter
// obj_tail is NULL when no nodes are connected to the obj_head/obj_tail
if (temp->obj_tail != NULL) {
temp->obj_tail->next = n;
n->prev = temp->obj_tail;
n->next = NULL;
temp->obj_tail = n;
}
else {
n->next = NULL;
n->prev = NULL;
// This is where obj_head and obj_tail are assigned and cannot be NULL.
temp->obj_head = n;
temp->obj_tail = n;
}
}
}