2012-09-29 48 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