Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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++ 用于保存结构指针的STL列表_C++_List_Stl_Pointers_Segmentation Fault - Fatal编程技术网

C++ 用于保存结构指针的STL列表

C++ 用于保存结构指针的STL列表,c++,list,stl,pointers,segmentation-fault,C++,List,Stl,Pointers,Segmentation Fault,我有一个叫做vertex的结构,我创建了一些指向它们的指针。我想做的是将这些指针添加到列表中。下面的代码,当它试图将指针插入列表时,创建了一个分段错误。有人能解释一下发生了什么事吗 #include <iostream> #include <list> #define NUM_VERTICES 8 using namespace std; enum { WHITE, GRAY, BLACK }; struct vertex { int color;

我有一个叫做vertex的结构,我创建了一些指向它们的指针。我想做的是将这些指针添加到列表中。下面的代码,当它试图将指针插入列表时,创建了一个分段错误。有人能解释一下发生了什么事吗

#include <iostream>
#include <list>

#define NUM_VERTICES 8

using namespace std;

enum { WHITE, GRAY, BLACK };

struct vertex
{
    int color;
    int distance;
    char parent;
};

int main()
{
    //create the vertices
    vertex r = {WHITE, NULL, NULL};

    //create pointer to the vertex structures
    vertex *pr = &r;

    //create a list to hold the vertices
    list<vertex*> *r_list = new list<vertex*>;

    list<vertex*>::iterator it;

    r_list->insert(it, pr);
}
#包括
#包括
#定义NUM_顶点8
使用名称空间std;
枚举{白色、灰色、黑色};
结构顶点
{
内色;
整数距离;
半焦父代;
};
int main()
{
//创建顶点
顶点r={WHITE,NULL,NULL};
//创建指向顶点结构的指针
顶点*pr=&r;
//创建一个列表以保存顶点
列表*r_列表=新列表;
列表::迭代器;
r_列表->插入(it、pr);
}

您尚未初始化它,因此您正在随机/未初始化的位置/指针插入


std::list
添加项目的常规方法包括其方法
push_back
push_front
;通常,只有在您之前确定了要在其中插入一个项目的特定位置时,才会使用
insert

首先,您不会将
初始化为任何内容。你是说:

list<vertex*>::iterator it = r_list->begin();

您尚未初始化迭代器,因此使用插入无效。例如,您可以使用
r\u list->push\u back(pr)


此外,一旦r超出范围,列表中的指针将不再有效。显然,这在本例中不是问题,因为它位于
main()
,但我假设这不是您将要使用代码的确切示例,因此它可能会回来咬您……

这里有一些错误

首先,您没有像其他人所说的那样初始化迭代器:

list<vertex*>::iterator it = r_list->begin();
此外,使用迭代器进行插入的工作还有很长的路要走。仅使用或:

如果给vertex一个构造函数,您甚至可以就地构造它们:

struct vertex
{
    int color;
    int distance;
    char parent;

    vertex(int _color, int _distance, char _parent) :
    color(_color),
    distance(_distance),
    parent(_parent)
    {
    }
};

//create a list to hold the vertices
list<vertex> r_list;

r_list.push_back(vertex(WHITE, NULL, NULL));
其次,使用
常量
而不是
#定义

#define NUM_VERTICES 8 // <- bad
const int NumberVertices = 8; // <- good

希望这些帮助

当我试图使用push_back()时,mingw编译器出现了这个错误。错误:调用'std::list::push_back(vertex&')时没有匹配的函数。但是,当我用g++编译它时,它没有给我这个错误。无论如何,我认为这个主题属于另一个线程。这个编译器错误看起来像是您试图将顶点而不是顶点*传递到push_-back方法中。但我可能错了……没错,汤姆。谢谢我需要在阅读错误方面做得更好,这需要时间,但你会做得更好。当你看到像“std::allocator”之类的冗长的东西时,不要害怕(很多时候你会看到更长的东西)。过滤垃圾并获取其实质:您有一个列表,要推回的参数表示vertex&.:-)。很好的汇编了所有的细节。我很高兴您强调了指针的生命周期以及在堆栈和堆上分配的区别。您在主函数中缺少了一个返回值
//create a list to hold the vertices
list<vertex*> r_list;

r_list.push_back(pr);
// global
list<vertex*> r_list;

void some_function(void)
{
    //create the vertices
    vertex r = {WHITE, NULL, NULL};

    //create pointer to the vertex structures
    vertex *pr = &r;

    r_list.push_back(pr);
} // right here, vertex r stops existing: the list now contains an
  // invalid pointer.
// global
list<vertex*> r_list;

void some_function(void)
{
    //create the vertices
    vertex *r = new vertex;
    r->color = WHITE;
    r->distance = 0;
    r->parent = 0;

    r_list.push_back(r);
}
//create a list to hold the vertices
list<vertex> r_list;

//create the vertices
vertex r = {WHITE, NULL, NULL};

r_list.push_back(r);
struct vertex
{
    int color;
    int distance;
    char parent;

    vertex(int _color, int _distance, char _parent) :
    color(_color),
    distance(_distance),
    parent(_parent)
    {
    }
};

//create a list to hold the vertices
list<vertex> r_list;

r_list.push_back(vertex(WHITE, NULL, NULL));
//create the vertices
vertex r = {WHITE, 0, 0};
#define NUM_VERTICES 8 // <- bad
const int NumberVertices = 8; // <- good
enum Color { WHITE, GRAY, BLACK };