C++;将类对象传递给函数 我对C++编程很陌生。 现在我有国家班了。我想用这个类创建相邻的状态,所以我将函数getNeightringState()添加到我的类中。在这个函数中,我将“邻接_状态”传递给函数set_neighting_state(),这个函数改变“neighting_states”的值。 在这个函数中,我设置了一个for循环进行测试。它打印出“7103664528”,这是我想要的值。但是在函数getNeightringState()中,我还设置了一个for循环,该循环的任务与set_Neighting_state()中的相同,但屏幕显示“0 1 4716672 2686652 2686528 0 4716676 4519501 4716676”

C++;将类对象传递给函数 我对C++编程很陌生。 现在我有国家班了。我想用这个类创建相邻的状态,所以我将函数getNeightringState()添加到我的类中。在这个函数中,我将“邻接_状态”传递给函数set_neighting_state(),这个函数改变“neighting_states”的值。 在这个函数中,我设置了一个for循环进行测试。它打印出“7103664528”,这是我想要的值。但是在函数getNeightringState()中,我还设置了一个for循环,该循环的任务与set_Neighting_state()中的相同,但屏幕显示“0 1 4716672 2686652 2686528 0 4716676 4519501 4716676”,c++,function,pointers,reference,functional-programming,C++,Function,Pointers,Reference,Functional Programming,我不知道我的代码出了什么问题。我现在需要做什么 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 i

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

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;
}
int n;//板的列数和行数
int k;//要使用的启发式函数的类型
int tilescont;//瓦片的数量,包括空白的数量
int statesunt;//生成的状态数
int*m_;
int*m_goalTiles;
国际电信公司;
常数int UP=0;
常数int DOWN=1;
const int RIGHT=2;
常数int左=3;
int*方向;
阶级国家{
公众:
状态(){}
int getBlankTilePosition(){
对于(int i=0;i设置\u块(相邻\u块);
//此循环用于打印“7 1 0 3 6 4 5 2 8”
对于(int i=0;i>m_initTiles[i];
对于(int i=0;i>m_goalTiles[i];
对于(int i=0;i从上述代码段中删除
int-neighbor_tiles[n*n];
行,并使其对所有函数全局可用,因此将其声明为非函数类的数据,即
添加int-neighbor_tiles[n*n];将类作为数据类型。

在询问之前,请先使用适当的调试器进行调试。
邻居tiles
是一个局部变量。您正在存储指向其第一个元素的指针。当函数返回时,这些指针将无效。如果可能,请使用
std::vector
。我只是试图使帖子尽可能详细修改他们的代码,如果你把代码裁剪成你所更改的内容,以及为什么需要更改(即他们的方式有什么问题),这将对OP有所帮助。@DarkKnight建议:不要过多使用全局变量。你应该尽可能少地使用全局变量。
int blankPosition = getBlankTilePosition();
int neighbor_tiles[n * n];