c++ - 如何在下面的代码中返回指向children[poz]的父节点的指针?
问题描述
我试图在八叉树中找到一个特定的 RGB 点(在我已经插入它之后),我希望这个函数返回一个指向该节点的父节点的指针或一个包含该节点及其兄弟的列表。我怎样才能改变这个代码来得到它?此外,当遇到空节点时,我尝试返回 nullptr 或 NULL,但我得到一个编译错误:没有从返回类型“nullptr_t”到函数返回类型“vector<Octree *>”的可行转换,我该如何解决?
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;
}
}
解决方案
错误消息是一个线索。你的方法返回一个向量,所以你不能返回一个指针。
您可以更改方法的返回类型,也可以创建并返回一个空向量。
在不知道数据结构的情况下,我没有关于如何返回父级的绝对答案,但是您可以在类中保留父级知识或将父级作为可选参数传递给方法。
推荐阅读
- python - 如何从另一行和列的 idxmin 获取列值?
- python - 使用 brew 安装 openssl 后 Flask 无法运行错误
- docker-compose - 设置 gRPC DB-less API 网关:415 Unsupported Media Type
- get - Pouch DB - 创建的数据库 - 保存在 _id 下 - 尝试检索以后的更改数据并重新保存
- azure-cosmosdb - 如果您尝试在强一致性 cosmos DB 中写入后立即读取数据,会发生什么情况?
- javascript - 使用另一个页面的 div 弹出小窗口
- ios - 无法使用 WKWebView 在 Safari 中打开外部链接
- php - 将谷歌地图商店定位器转换为 laravel 不起作用
- react-native - Typeerror:React-native android中的网络请求失败
- javascript - 如何在角度单元测试中对服务进行单元测试?