C++ 如何初始化指针向量?

C++ 如何初始化指针向量?,c++,pointers,vector,C++,Pointers,Vector,我的程序在尝试运行函数initialize_graph时崩溃。我省略了所有其他功能。这是我第一次使用向量。图表.h标题将位于main下方。我只需要有人帮我初始化一个头部指针为空的图形对象的向量。我可以从那里得到它。谢谢你的关注 #include <cstdlib> #include <iostream> #include <vector> #include "graph.h" using namespace std; vector<graph*>

我的程序在尝试运行函数initialize_graph时崩溃。我省略了所有其他功能。这是我第一次使用向量。图表.h标题将位于main下方。我只需要有人帮我初始化一个头部指针为空的图形对象的向量。我可以从那里得到它。谢谢你的关注

#include <cstdlib>
#include <iostream>
#include <vector>
#include "graph.h"

using namespace std;

vector<graph*> adj_list; // one dimensional vector; each position in the vector stores a pointer to the head of a linked list


void graph::initialize_graph(int num_of_vertices, int num_of_edges)
{
    cout << "Doing push back"; // want to push back the same obj. call it graph* graph_obj (where head is null)
    //adj_list.resize(num_of_vertices); 
    graph *graph_obj;
    graph_obj -> head = NULL;
    for(int k = 0; k < num_of_vertices; k++)
    {
        adj_list.push_back(graph_obj); 
    }
    cout << "Pushback complete";
}

int main()
{
    int num_of_vertices, num_of_edges, vertex1, vertex2, function;
    graph graph_obj;


    while(1)
    {

     cout<<"1 - initialize graph" <<endl;
     cout<<"2 - insert an edge to the graph" <<endl;
     cout<<"3 - delete an edge from the graph" <<endl;
     cout<<"4 - list all edges in the graph" <<endl;
     cout<<"5 - list all of the neighbors for a particular vertex" << endl;
     cout<<"6 - list all of the vertices with no incoming edges" << endl << endl;

    cout<<"Choose a function (1 - 6): ";
    cin>>function;
    cout<<endl<<endl;

    switch(function)
    {
      case 1: 
             cout<<"Enter the number of vertices in the graph: ";
             cin>>num_of_vertices;
             cout<<endl<<"Enter the number of edges in the graph: ";
             cin>>num_of_edges;
             cout<<endl<<endl;
             cin.get();
             graph_obj.initialize_graph(num_of_vertices, num_of_edges);
             break;

     case 2: 
            cout<<"To enter an edge X -> Y (an edge from node X to node Y), use the following format: X Y (the names of the two vertices separated by a single space)" << endl;
            cout<<"Enter the edge to insert into the graph: ";
            cin>>vertex1>>vertex2;
           cout<<endl<<endl;
           graph_obj.insert_edge(vertex1, vertex2);         
           break;

     case 3: 
            cout<<"To enter an edge X -> Y (an edge from node X to node Y), use the following format: X Y (the names of the two vertices separated by a single space)" << endl;
            cout<<"Enter the edge to delete from the graph: ";
            cin>>vertex1>>vertex2;
            cout<<endl<<endl;
           graph_obj.delete_edge(vertex1, vertex2);         
           break;

     case 4:
             graph_obj.list_all_edges(num_of_vertices);
             break;

     case 5:   
            cout<<"Enter the vertex to list all of the neighbors for: ";
            cin>>vertex1;
            cout<<endl<<endl;
            graph_obj.list_all_neighbors(vertex1, num_of_vertices);
            break;

     case 6:
           graph_obj.no_incoming_edges(num_of_vertices);


    } //end switch



    }  //end while

    system("PAUSE");
    return 0;
}

class graph //class for the graph data structure
{
    public:  
     void initialize_graph(int num_of_vertices, int num_of_edges); //creates a new directed graph
     void insert_edge(int vertex1, int vertex2);  // inserts a directed edge (V1 - > V2) into the graph
     void delete_edge(int vertex1, int vertex2);    // deletes an edge (V1 -> V2) from the graph
     void list_all_edges(int num_of_vertices); // lists all of the edges in the graph
     void list_all_neighbors(int vertex1, int num_of_vertices); // lists all of the neighbors for a particular vertex
     void no_incoming_edges(int num_of_vertices);  // lists all of the vertices with no incoming edges

    private: 
    graph *prev; //pointer to the previous node in the linked list (used in the adjacency list implementation only)
    graph *next;  //pointer to the next node in the linked list    (used in the adjacency list implementation only)
    graph *head; //pointer to the head of a linked list            (used in the adjacency list implementation only)
    int edge;   // the id of the vertex that there is an edge to    (used in both implementations)

};
#包括
#包括
#包括
#包括“graph.h”
使用名称空间std;
向量调整列表;//一维向量;向量中的每个位置都存储指向链接列表头部的指针
void graph::initialize_graph(顶点的int num_,边的int num_)
{
cout head=NULL;
对于(int k=0;k这里可能有点问题:

graph *graph_obj;
graph_obj -> head = NULL;

您正在创建一个未初始化的指针,然后立即取消激活它。您需要为该指针创建一个指向的对象。

这里有点问题:

graph *graph_obj;
graph_obj -> head = NULL;

您正在创建一个未初始化的指针,然后立即取消激活它。您需要为该指针创建一个指向的对象。

这里有点问题:

graph *graph_obj;
graph_obj -> head = NULL;

您正在创建一个未初始化的指针,然后立即取消激活它。您需要为该指针创建一个指向的对象。

这里有点问题:

graph *graph_obj;
graph_obj -> head = NULL;

您正在创建一个未初始化的指针,然后立即取消激活它。您需要为该指针创建一个指向的对象。

您从未在以下位置从堆中为指针实际分配新内存:

graph *graph_obj;
它的指针值是未定义的,这就是它崩溃的原因

将其初始化为:

graph *graph_obj = new graph(); // Depending on your constructor.

编辑:另外,如果要重新使用此指针并将其推回到容器中,则需要为容器中的每个元素创建一个新指针(以后不要忘记删除它!)。

您从未在以下位置从堆中为指针分配过新内存:

graph *graph_obj;
它的指针值是未定义的,这就是它崩溃的原因

将其初始化为:

graph *graph_obj = new graph(); // Depending on your constructor.

编辑:另外,如果要重新使用此指针并将其推回到容器中,则需要为容器中的每个元素创建一个新指针(以后不要忘记删除它!)。

您从未在以下位置从堆中为指针分配过新内存:

graph *graph_obj;
它的指针值是未定义的,这就是它崩溃的原因

将其初始化为:

graph *graph_obj = new graph(); // Depending on your constructor.

编辑:另外,如果要重新使用此指针并将其推回到容器中,则需要为容器中的每个元素创建一个新指针(以后不要忘记删除它!)。

您从未在以下位置从堆中为指针分配过新内存:

graph *graph_obj;
它的指针值是未定义的,这就是它崩溃的原因

将其初始化为:

graph *graph_obj = new graph(); // Depending on your constructor.



编辑:另外,如果要重新使用此指针并将其推回容器,则需要为容器中的每个元素创建一个新指针(以后不要忘记删除它!)。

崩溃?我认为
推回(*graph\u obj)
甚至应该编译。我的错误。我正在测试一些东西,但忘了将它们改回原来的样子。即使使用下面的答案,你的循环也会将相同的指针值推到向量上。这是你的意图吗?一个填充了相同值的向量?@PaulMcKenzie是的,这是正确的。我正在处理图形,需要做一个调整邻接列表。我需要一个节点上的向量类型图,其头部指向NULL。@Eidbanger-如果是这种情况,祝您好运,无问题地释放内存。我可以看到您将来出现双重释放错误问题。崩溃?我不认为
推回(*graph\u obj)
甚至应该编译。我的错误。我正在测试一些东西,但忘了将它们改回原来的样子。即使使用下面的答案,你的循环也会将相同的指针值推到向量上。这是你的意图吗?一个填充了相同值的向量?@PaulMcKenzie是的,这是正确的。我正在处理图形,需要做一个调整邻接列表。我需要一个节点上的向量类型图,其头部指向NULL。@Eidbanger-如果是这种情况,祝您好运,无问题地释放内存。我可以看到您将来出现双重释放错误问题。崩溃?我不认为
推回(*graph\u obj)
甚至应该编译。我的错误。我正在测试一些东西,但忘了将它们改回原来的样子。即使使用下面的答案,你的循环也会将相同的指针值推到向量上。这是你的意图吗?一个填充了相同值的向量?@PaulMcKenzie是的,这是正确的。我正在处理图形,需要做一个调整邻接列表。我需要一个节点上的向量类型图,其头部指向NULL。@Eidbanger-如果是这种情况,祝您好运,无问题地释放内存。我可以看到您将来出现双重释放错误问题。崩溃?我不认为
推回(*graph\u obj)
甚至应该编译。我的错误。我正在测试一些东西,但忘了将它们改回原来的样子。即使使用下面的答案,你的循环也会将相同的指针值推到向量上。这是你的意图吗?一个填充了相同值的向量?@PaulMcKenzie是的,这是正确的。我正在处理图形,需要做一个调整邻接列表。我需要一个节点上的向量类型图,其头部指向NULL。@Eidbanger-如果是这种情况,祝你好运,在没有问题的情况下释放内存。我可以看到你将来会出现双重释放错误问题。我想我需要在某个地方使用“new”调用?@Eidbanger:似乎有可能。我想我需要在某个地方使用“new”调用@Eidbanger:似乎有可能。我想我需要在某个地方使用“new”调用?@Eidbanger:似乎有可能。我想我需要在某个地方使用“new”调用?@Eidbanger:似乎有可能。好的,谢谢!但是在graph\u obj初始化之后,说graph\u obj->head=NULL是否正确?只要后续的graph没有