2014-04-22 69 views
2

我想动态地创建一个大小为'n'的数组,其中可以包含指向多个邻接列表的指针。然而,当我运行我的代码,它不会导致节点的阵列,它只是导致了这一点:如何动态创建指针数组?

Netbeans debugging variables

这不是一个阵列,它只是一个节点。

这里是我的代码:

的main.cpp

#include "graph.h" 
using namespace std; 

int main() { 
    graph g; 
    return 0; 
} 

graph.cpp

#include "graph.h" 

// Constructor 
graph::graph() 
{ 
    int size = 5; // 
    // Allocate array of size (n) and fill each with null 
    adjListArray = new node*[size]; 

    // Set each entry in array to NULL 
    for (int i = 0; i < size; i++) 
    { 
     adjListArray[i] = NULL; 
    } 
} 

graph.h

#include<cassert> 
#include<iostream> 
#include<string> 
#include<fstream> 
using namespace std; 

struct node 
{ 
    int name; 
    node *next; 
}; 

class graph 
{ 
    public: 
     // Constructors/destructors 
     graph(); 
     ~graph(); 

    private: 
     node** adjListArray; // Array of pointers to each adjacency list 
}; 

我要去哪里错了?

+1

不知道我看到任何问题。这可能是您的调试器代表数据的问题......以及是否可以强制它将adjListArray显示为数组。请记住,你显示的属性没有被声明为一个数组,而是作为'node **'声明的。 – jsantander

回答

1

问题是,当你只有一个指向内存开始的指针时,没有办法告诉内存有多大,所以你的IDE只是假设它的大小是sizeof(datatype),只显示第一个元件。

+0

我非常愚蠢地盲目地相信调试器,而不是测试自己是否存在这些值。问题解决了,谢谢。 – mb595x