2012-09-29 50 views
0

我有我填写的指针数组有两个指针的函数:返回指针的指针数组获得下一个元素失败

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]中指出的正确元素?

它太讨厌了。

Cheers Knut

回答

0

问题在于第一个函数。你正在返回一个指针数组的第一个元素。我认为你的意图是返回两个元素的数组,以便索引0和1访问你在第一个函数中分配的指针。使用现有的数据结构没有简单的修复。一种解决方案是让第一个函数返回一个std :: pair。将两个指针分配到pair.first和pair.second中,然后稍后便可轻松访问这些值。

+0

嗨,谢谢你的回答。我试着将它作为Vertex **返回,但之后我稍后丢失了这些值。我无法弄清楚,他们为什么会失望。他们没有被覆盖。这是呼叫的相同意思。但用**代替。我会再次更改它,看看会发生什么 – knut

+0

顶点**不会工作,因为您将返回数组的地址,这是一个局部变量。 – combinatorial

+0

我现在只使用第一个顶点,并计算其余的由于这个。 :d – knut