包含相邻顶点列表的自定义顶点类型[图形]:不完整类型错误(Custom Vertex type [graphs] containing list of adjacent vertices: incom

编程入门 行业动态 更新时间:2024-10-26 14:29:23
包含相邻顶点列表的自定义顶点类型[图形]:不完整类型错误(Custom Vertex type [graphs] containing list of adjacent vertices: incomplete type error)

我正在实现我自己的一组用于表示图形的类。 我是C ++的新手,所以我在使用指针时遇到了一些麻烦。 你看,这是我原来的代码:

class Vertex { public: int label; Vertex adjacent_vertices[]; Vertex(int l) : label(l) { } Vertex(int l, Vertex adjacents[]) : label(l), adjacent_vertices(adjacents) { } };

但后来我得到了一个不完整的类型错误。 根据我的研究,我显然需要使用adjacent_vertices的指针列表。 Vertex* adjacent_vertices[]; 但是第二个构造函数存在问题。 这些都不起作用:

adjacent_vertices(*adjacents) adjacent_vertices(adjacents*) adjacent_vertices(&adjacents)

有没有更好的方法来实现这个? 我觉得这里有一些我非常明显的东西,作为Java / Python开发人员之前从未使用过指针。

I'm implementing my own set of classes for representing graphs. I'm new to C++, so I'm having some trouble with pointers. You see, this was my original code:

class Vertex { public: int label; Vertex adjacent_vertices[]; Vertex(int l) : label(l) { } Vertex(int l, Vertex adjacents[]) : label(l), adjacent_vertices(adjacents) { } };

but then I get an incomplete type error. From my research, I apparently need to use a list of pointers for adjacent_vertices. Vertex* adjacent_vertices[]; But then there's a problem with the second constructor. None of these worked:

adjacent_vertices(*adjacents) adjacent_vertices(adjacents*) adjacent_vertices(&adjacents)

Is there a better way to implement this? I feel like there's something very obvious I'm missing here, as a Java/Python dev who's never worked with pointers before.

最满意答案

一个简单的解决方案是使用矢量作为您的adjacent_vertices。

class Vertex { public: int label; vector<Vertex> adjacent_vertices; Vertex(int l) : label(l) { } Vertex(int l, vector<Vertex> adjacents) : label(l), adjacent_vertices(adjacents) { } };

An easy solution would be to use vector for your adjacent_vertices.

class Vertex { public: int label; vector<Vertex> adjacent_vertices; Vertex(int l) : label(l) { } Vertex(int l, vector<Vertex> adjacents) : label(l), adjacent_vertices(adjacents) { } };

更多推荐

本文发布于:2023-08-04 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顶点   类型   自定义   不完整   图形

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!