C++ C++;对象向量:指定对象
我必须为一个类制作一个十六进制板,我的代码有问题。我的图形有一个板节点的2D向量。每个节点都有一个相邻节点的向量。我似乎无法将相邻节点分配到向量中 节点类:C++ C++;对象向量:指定对象,c++,vector,graph,C++,Vector,Graph,我必须为一个类制作一个十六进制板,我的代码有问题。我的图形有一个板节点的2D向量。每个节点都有一个相邻节点的向量。我似乎无法将相邻节点分配到向量中 节点类: class node{ public: string hex_type = "E";// empty to start vector<node> neighbors; int Xcoordinate; int Ycoordinate; class graph{ public: int
class node{
public:
string hex_type = "E";// empty to start
vector<node> neighbors;
int Xcoordinate;
int Ycoordinate;
class graph{
public:
int size;
graph() { this->size = 11; }
graph(int a){ this->size = a; }
vector<vector<node> > nodes;
void initialize(){
int x, y;
int max = this->size-1;
this->nodes = vector<vector<node> >(size, vector <node>(size));
for (x = 0; x < size; ++x){
for (y = 0; y < size; ++y){
//this->nodes[x][y] = node();
this->nodes[x][y].Xcoordinate = x;
this->nodes[x][y].Ycoordinate = y;
this->nodes[x][y].neighbors = vector<node>(6);
if ((x == 0) && (y == 0)){ this->nodes[x][y].neighbors[0] = this->nodes[x + 1][y]; }
}
}
}
};
类节点{
公众:
字符串hex_type=“E”//开始时为空
向量邻域;
国际协调;
国际协调;
类图{
公众:
整数大小;
graph(){this->size=11;}
图(inta){this->size=a;}
向量节点;
void initialize(){
int x,y;
int max=此->大小-1;
这->节点=向量(大小,向量(大小));
对于(x=0;x节点[x][y]=node();
这->节点[x][y].Xcoordinate=x;
这->节点[x][y].Ycoordinate=y;
这->节点[x][y]。邻居=向量(6);
如果((x==0)和(&(y==0)){this->nodes[x][y]。邻居[0]=this->nodes[x+1][y];}
}
}
}
};
我这里的打印语句只输出一系列数字:-84215051
cout << this->nodes[0][0].neighbors[0].Xcoordinate;
cout节点[0][0]。邻居[0]。Xcoordinate;
。邻居[0]=this->nodes[x+1][y];
正在制作节点的副本。尚未初始化的节点的副本。因此,该副本具有无意义的值
您可能需要向量邻居;
,因此它保存指向其邻居的指针,而不是副本
如果是的话,那就是
.neighbors[0] = &(this->nodes[x + 1][y]);
^
当执行此->节点[x][y]。邻居[0]=此->节点[x+1][y];时,x=0,y=0。节点[1][0]尚未初始化。 可以首先初始化所有节点
for (x = 0; x < size; ++x){
for (y = 0; y < size; ++y){
//this->nodes[x][y] = node();
this->nodes[x][y].Xcoordinate = x;
this->nodes[x][y].Ycoordinate = y;
}
}
for (x = 0; x < size; ++x){
for (y = 0; y < size; ++y){
//this->nodes[x][y] = node();
this->nodes[x][y].neighbors = vector<node>(6);
if ((x == 0) && (y == 0)){ this->nodes[x][y].neighbors[0] = this->nodes[x + 1][y]; }
}
}
(x=0;x这是如何协调的?代码行:this->nodes[x][y]。邻居[0]=this->nodes[x+1][y]将更改为。。。。。?