gpt4 book ai didi

c++ - 尝试初始化位于区域结构内的边缘结构。我怎么做?

转载 作者:太空狗 更新时间:2023-10-29 21:15:40 25 4
gpt4 key购买 nike

这是内部有边缘结构的区域结构。

    /**
* district struct that keeps track of the name, valence, and connections a district has to others
*/
struct district {
/**
* contains an edge with two vertices
*/
struct edge {
district *vertex1, *vertex2;
/**
* initializes both vertices to nullptr
*/
edge() :
vertex1(nullptr), vertex2(nullptr) {
}
};
T name;
int valence;
edge *connection[100];
int color;
/**
* initializes all struct members to default values
*/
district(T name) :
name(name), valence(0), connection { nullptr }, color(100) {
}
};

我正在尝试像这样在顶点之间创建边:

    list[i]->connection[list[i]->valence]->vertex1 = list[i];
list[i]->connection[list[i]->valence]->vertex2 = list[j];
list[i]->valence++; //add 1 to the amount of vertices for district a
list[j]->connection[list[j]->valence]->vertex1 = list[j];
list[j]->connection[list[j]->valence]->vertex2 = list[i];
list[j]->valence++; //add 1 to the amount of vertices for district b
sort(); //sort the list in order of valence

但据我所知,为了将数据写入该边缘,需要首先使用“new”运算符创建它。这些地区已经在代码中进一步初始化到它们在列表数组中的相应位置(如果它们不存在的话),我不需要这方面的帮助。

我尝试了几种不同的方法来创建新边缘:

    list[i]->connection[list[i]->valence] = new district.edge;
list[i]->connection[list[i]->valence] = new district->edge;
list[i]->connection[list[i]->valence] = new edge;

但是它们都不起作用。我该如何实现?

最佳答案

new district.edge

不,district 不是对象。

new district->edge

不,district 不是指针。

new edge

范围内没有名为 edge 的类型。 (除非您在 district 的成员函数中执行此操作。)

相反,使用范围解析运算符:::

new district::edge

关于c++ - 尝试初始化位于区域结构内的边缘结构。我怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com