C++ 如何在下面的代码中返回指向子节点[poz]的父节点的指针?
我试图在八叉树中找到一个特定的RGB点(在我已经插入它之后),我希望这个函数返回一个指向该节点父节点的指针或一个包含该节点及其兄弟节点的列表。我如何更改此代码以获得该值?此外,当遇到空节点时,我尝试返回nullptr或NULL,但得到一个编译错误:从返回的类型为“nullptr_t”的值到函数返回类型为“vector”的值没有可行的转换,我如何解决这个问题C++ 如何在下面的代码中返回指向子节点[poz]的父节点的指针?,c++,octree,C++,Octree,我试图在八叉树中找到一个特定的RGB点(在我已经插入它之后),我希望这个函数返回一个指向该节点父节点的指针或一个包含该节点及其兄弟节点的列表。我如何更改此代码以获得该值?此外,当遇到空节点时,我尝试返回nullptr或NULL,但得到一个编译错误:从返回的类型为“nullptr_t”的值到函数返回类型为“vector”的值没有可行的转换,我如何解决这个问题 vector<Octree*> Octree::find(int R, int G, int B) { int
vector<Octree*> Octree::find(int R, int G, int B)
{
int midR = (topLeftFront->R
+ bottomRightBack->R)
/ 2;
int midG = (topLeftFront->G
+ bottomRightBack->G)
/ 2;
int midB = (topLeftFront->B
+ bottomRightBack->B)
/ 2;
int pos = -1;
// Deciding the position
// where to move
if (R <= midR) {
if (G <= midG) {
if (B <= midB)
pos = TopLeftFront;
else
pos = TopLeftBottom;
}
else {
if (B <= midB)
pos = BottomLeftFront;
else
pos = BottomLeftBack;
}
}
else {
if (G <= midG) {
if (B <= midB)
pos = TopRightFront;
else
pos = TopRightBottom;
}
else {
if (B <= midB)
pos = BottomRightFront;
else
pos = BottomRightBack;
}
}
// If an internal node is encountered
if (children[pos]->point == nullptr) {
return children[pos]->find(R, G, B);
}
// If an empty node is encountered
else if (children[pos]->point->R == -1) {
return nullptr;
}
else {
// If node is found with
// the given value
if (R == children[pos]->point->R
&& G == children[pos]->point->G
&& B == children[pos]->point->B)
return children;
}
}
向量八叉树::查找(int R,int G,int B)
{
int midR=(上左前->右前
+右下角->右下角)
/ 2;
int midG=(topLeftFront->G
+右下角->G)
/ 2;
int midB=(topLeftFront->B
+右下角->B)
/ 2;
int pos=-1;
//决定职位
//搬到哪里去
如果(R点->G点
&&B==子项[pos]->点->B)
返回儿童;
}
}
错误消息是一条线索。您的方法正在返回向量,因此无法返回指针 您可以更改方法的返回类型,也可以创建并返回空向量 在不知道数据结构的情况下,对于如何返回父级,我没有绝对的答案,但是您可以在类中保留父级知识,或者将父级作为可选参数传递给方法