返回指向指针数组的指针获取下一个元素失败
Returning Pointer to an Array of Pointers get next element fail
我有一个函数,其中我用两个指针填充指针数组:
Vertex* MacePiece::getPositionOffset(int aDirection){
// returns Plate at Position aParent.myPlates[0]
BasePlate** theGroundPlate = this->getPlates();
Vertex* theOffset[] = {0,0};
switch(aDirection){
theOffset[0] = theGroundPlate[0]->getVertexAt(0); //A
theOffset[1] = theGroundPlate[0]->getVertexAt(1); //B
break;
}
return theOffset[0];
}
接下来,我将带有第一个元素地址的指针传递给另一个函数:
Vertex* theParentOffset = aParentPiece->getPositionOffset(aDirection);
theConnectingPiece->setPositionInMace(theParentOffset, aDirection);
在这里,我使用指向的元素如下:
void MacePiece::setPositionInMace(Vertex* aOffset, int aDirection){
// set groundplate position
updateGroundPlatePositionByOffsetVertex(aOffset, aDirection);
}
void MacePiece::updateGroundPlatePositionByOffsetVertex(Vertex* aOffset, int aDirection) {
BasePlate** thePlates = this->getPlates();
// first update GroundPlate's Position
Vertex* A = thePlates[0]->getVertexAt(0);
Vertex* B = thePlates[0]->getVertexAt(1);
Vertex* C = thePlates[0]->getVertexAt(2);
Vertex* D = thePlates[0]->getVertexAt(3);
switch(aDirection){
case MOVE_NORTH:
// still 3D thus 2D's y is 3D'z if camera is looking down Y
// Two Sides are connecting thus Some Plate locations are identical
// A.x defined by parent D.x
A->position[0] = aOffset[1].position[0];
// A.z defined by parent D.z
A->position[2] = aOffset[1].position[2];
//B.x defined by parent C.x
B->position[0] = aOffset[0].position[0];
//B.z defined by parent C.z
B->position[2] = aOffset[0].position[2];
// now set Opposit side (A->D, B->C), since moving north:
// x is the same
// z is reduced by -1 because OpenGL's Z is reducing to far-pane
D->position[0] = A->position[0];
D->position[2] = A->position[2] - 1.0f;
C->position[0] = B->position[0];
C->position[2] = B->position[2] - 1.0f;
break;
具体来说,这部分:
// A.x defined by parent D.x
A->position[0] = aOffset[1].position[0];
// A.z defined by parent D.z
A->position[2] = aOffset[1].position[2];
//B.x defined by parent C.x
B->position[0] = aOffset[0].position[0];
//B.z defined by parent C.z
B->position[2] = aOffset[0].position[2];
aOffset[1] 返回与 aOffset[0] 相同的值
当我传递指向每个信息的数组的 ** 指针时,都会丢失。
我正在创造一种迷宫,它只是在一个方向上失败了。传递该指针有什么问题?如何在偏移量[1]中指向正确的元素?
它是如此的厌恶。
干杯克努特
问题出在第一个函数上。您返回数组的第一个元素,该元素是一个指针。我认为您的意图是返回两个元素的数组,以便索引 0 和 1 访问您在第一个函数中分配的指针。使用现有数据结构没有简单的修复方法。一种解决方案是让第一个函数返回 std::p air。将两个指针分配给 pair.first 和 pair .second,然后以后可以轻松访问这些值。
相关文章:
- 如何将指针从一个void函数传递到另一个C++
- 如何在没有数据拷贝的情况下从指针创建一个Eigen VectorXd对象
- 重新定位图像时如何前进到下一个内存块
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 利用双重链接列表中的下一个指针
- 将原始指针移动到已创建函数中的下一个字符
- 指针指向数组中的下一个元素,但地址是相同的.如何
- 返回指向指针数组的指针获取下一个元素失败
- strtok() - 为什么必须传递 NULL 指针才能获取字符串中的下一个标记
- 如何使运算符++继续到链表中的下一个节点指针
- 包含指向下一个的指针的排序结构
- 指向下一个地址的指针
- 只是尝试插入一个节点并将指针设置为下一个节点C++
- Node类中公共下一个指针的分支
- 将C++函数重写为C#(将指针传递到数组中的下一个元素)
- 删除c++中的链接列表下一个指针
- 创建泛型链表,将下一个指针放在结构的开头
- 当我访问下一个Node指针时,c++崩溃了
- 在链表中插入似乎将下一个指针设置错误