2014-12-03 244 views
-1

我是C++编程的新手。 现在我有国家班。我想用这个类创建邻居状态,所以我将getNeighboringStates()函数添加到我的类中。在这个函数中,我传入“neighbor_states”来设置函数set_neighboring_state(),这个函数改变“adjacent_states”的值。 在这个函数中,我设置了一个for循环来测试。它打印出“7 1 0 3 6 4 5 2 8”,这是我想要的值。但是在函数getNeighboringStates()中,我还设置了一个for循环,它具有与set_neighboring_state()中相同的任务,但屏幕显示“0 1 4716672 2686652 2686528 0 4716676 4519501 4716676”。C++将类对象传递给函数

我不知道我的代码有什么问题。我现在需要做什么?

int n; // The number of columns as well as rows of the board 
int k; // The kind of heuristic function to use 
int tilesCount; // The number of tiles, including the blank one 
int statesCount; // The number of states generated 

int* m_initTiles; 
int* m_goalTiles; 
int tmpTile; 


const int UP = 0; 
const int DOWN = 1; 
const int RIGHT = 2; 
const int LEFT = 3; 
int* direction; 

class State { 
public: 


    State(){} 

    int getBlankTilePosition() { 
     for (int i = 0; i < n * n; i++) { 

      if (stateTiles[i] == 0) 
       return i; 

     } 
    } 

    void set_neighboring_state(State* neighboring_state, int direction) { 
     int blankPosition = getBlankTilePosition(); 
     int neighbor_tiles[n * n]; 

     for (int i = 0; i < n * n; i++) { 
      neighbor_tiles[i] = getStateTiles()[i]; 
     } 

     switch(direction) { 
     case UP: 
      if (blankPosition/n < 1) return; 
      else { 
       swap(neighbor_tiles[blankPosition], neighbor_tiles[blankPosition - n]); 
       neighboring_state->set_tiles(neighbor_tiles); 

       // This for loop print out "7 1 0 3 6 4 5 2 8" 
       for (int i = 0; i < n * n; i++) 
        cout << neighboring_state.getStateTiles()[i] << " "; cout << endl; 
      } 
      break; 
     case DOWN: 
      if (blankPosition/n == n - 1) return; 
      else { 
       swap(neighbor_tiles[blankPosition], neighbor_tiles[blankPosition + n]); 
       neighboring_state->set_tiles(neighbor_tiles); 
      } 
      break; 
     case LEFT: 
      if (blankPosition % n == 0) return; 
      else { 
       swap(neighbor_tiles[blankPosition], neighbor_tiles[blankPosition - 1]); 
       neighboring_state->set_tiles(neighbor_tiles); 
      } 
      break; 
     default: 
      if ((blankPosition + 1) % n == 0) return; 
      else { 
       swap(neighbor_tiles[blankPosition], neighbor_tiles[blankPosition + 1]); 
       neighboring_state->set_tiles(neighbor_tiles); 
      } 
      break; 
     } 
    } 

    /* 
    The maximum number of neighboring state that can be created is 4. 
    This function return the neighboring states of a certain state. 
    The first state represents for the "left" neighbor, the second, 
    the third and the fourth represent the "right", "up, and "down" 
    neighbor, respectively. 
    */ 
    State* getNeighboringStates() { 
     State* neighboring_states; 
     neighboring_states = new State[4]; 

     for (int i = 0; i < 4; i++) 
      set_neighboring_state(&neighboring_states[i], direction[i]); 

     // This print out "0 1 4716672 2686652 2686528 0 4716676 4519501 4716676" 
     cout << endl; 
     for (int i = 0; i < n * n; i++) 
      cout << neighboring_states[0].getStateTiles()[i] << " "; 
     cout << endl; 

     return neighboring_states; 
    } 



    State(int* pStateTiles) { 
     stateTiles = pStateTiles; 
    } 

    void set_tiles(int* tiles) { 
     stateTiles = tiles; 
    } 

    int* getStateTiles() { 
     return stateTiles; 
    } 

private: 
    int* stateTiles; 

}; 
void input(const char* fileName) { 
ifstream fin; 
fin.open(fileName); 

// read n, k from file 
fin >> n >> k; 

// allocate m_initTiles and m_goalTiles memory 
m_initTiles = new int[n * n]; 
m_goalTiles = new int[n * n]; 


for (int i = 0; i < n * n; i++) 
    fin >> m_initTiles[i]; 
for (int i = 0; i < n * n; i++) 
    fin >> m_goalTiles[i]; 

for (int i = 0; i < n * n; i++) 
    cout << m_initTiles[i] << " "; 
cout << endl; 
for (int i = 0; i < n * n; i++) 
    cout << m_goalTiles[i] << " "; 
cout << endl; 

fin.close(); 

} 

void initDirection() { 
direction = new int[4]; 
direction[0] = UP; 
direction[1] = DOWN; 
direction[2] = RIGHT; 
direction[3] = LEFT; 
} 

int main() { 

input("nPuzzle.inp"); 
initDirection(); 
State init_state (m_initTiles); 
State goal_state (m_goalTiles); 

State* init_neighbor = init_state.getNeighboringStates(); 
// int* state_tile = init_neighbor[0].getStateTiles(); 
// for (int i = 0; i < n * n; i++) 
// cout << state_tile[i] << " "; 

return 0; 
} 
+2

步骤通过声明为一个类的数据不是功能即。 – 2014-12-03 12:24:21

+0

'neighbor_tiles'是一个局部变量。您正在存储指向其第一个元素的指针。这些函数返回时变为无效。如果可能,请使用'std :: vector'。 – molbdnilo 2014-12-03 12:31:54

+0

我只是试图使帖子尽可能详细 – 2014-12-03 14:09:58

回答

0
int blankPosition = getBlankTilePosition(); 
int neighbor_tiles[n * n]; 

从上面的代码段删除int neighbor_tiles[n * n];线,使其全球提供给所有的功能,所以前问一个体面的调试器add int neighbor_tiles[n * n]; to class as an data type .

+0

如果您对其代码进行了修改,如果您将代码修改为专门针对您更改的内容以及为什么需要更改代码(即他们的方式有什么问题)。 – CoryKramer 2014-12-03 12:33:13

+0

感谢您的建议我试图评论,但不能.. – 2014-12-03 12:42:14

+0

非常感谢你,你已经节省了我吨的时间 – 2014-12-03 14:10:56