【问题标题】:Creating a list of nodes attached to each node (c++)创建附加到每个节点的节点列表(c++)
【发布时间】:2020-03-19 13:23:00
【问题描述】:

我正在尝试使用指向数组的指针创建每个节点,该数组将包含对象类中的其他节点(相同类型)。

class node
{
public:
    node(int degree, bool visited, int xpos, int ypos);
    node *adjList = new node[9]; 
    float * valueList = new float[832];
    int size = 0;
    int adjSize = 0;
    int xpos, ypos;
    int degree = 0;
    bool visited;
};

线是

node *adjList = new node[9]; 

这不起作用。这是说这个声明没有匹配的构造函数。我尝试创建一个空的构造函数,但这会破坏我的代码。

可视化是这样的:

节点: [0] ---> [ [3][4]][8] ]

【问题讨论】:

  • 没错。 new node[9] 创建九个 node 对象和默认构造它们。问题是没有node 默认构造函数。

标签: c++ arrays object constructor adjacency-list


【解决方案1】:

这一行

   node *adjList = new node[9];

creates 创建了九个 node 对象并尝试默认构造每个对象,因为您的 node 类中没有默认构造函数,编译器会抱怨。只需将默认构造函数添加到您的 node 类:

class node
{

   public:

       node(int degree, bool visited, int xpos, int ypos);

       node(); //Default ctor

       node *adjList = new node[9]; 
       float * valueList = new float[832];
       int size = 0;
       int adjSize = 0;
       int xpos, ypos;
       int degree = 0;
       bool visited;
};

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 2016-11-01
    • 2010-12-14
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多